I want to use Javascript Alert function in my ASP.NET page.
For example like this;
Response.Write("<script language=javascript>alert('ERROR');</script>);
But, this doesn't work.
I ask in here what am i doing wrong and everyone suggest me using RegisterScriptBlock
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('ERROR')",true);
But i don't want use it because it's working with PostBack
How can i do that without PostBack
?
EDIT: For example for using;
try
{
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConnectionString);
myConnection.Open();
string hesap = Label1.Text;
string musteriadi = DropDownList1.SelectedItem.Value;
string avukat = DropDownList2.SelectedItem.Value;
SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection);
cmd.Parameters.AddWithValue("@HESAP", hesap);
cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
cmd.Parameters.AddWithValue("@AVUKAT", avukat);
cmd.Connection = myConnection;
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
Response.Redirect(Request.Url.ToString());
myConnection.Close();
}
catch (Exception)
{
Response.Write("<h2>ERROR</h2>");
}
I want to use Javascript Alert function in my ASP.NET page.
For example like this;
Response.Write("<script language=javascript>alert('ERROR');</script>);
But, this doesn't work.
I ask in here what am i doing wrong and everyone suggest me using RegisterScriptBlock
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('ERROR')",true);
But i don't want use it because it's working with PostBack
How can i do that without PostBack
?
EDIT: For example for using;
try
{
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConnectionString);
myConnection.Open();
string hesap = Label1.Text;
string musteriadi = DropDownList1.SelectedItem.Value;
string avukat = DropDownList2.SelectedItem.Value;
SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection);
cmd.Parameters.AddWithValue("@HESAP", hesap);
cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
cmd.Parameters.AddWithValue("@AVUKAT", avukat);
cmd.Connection = myConnection;
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
Response.Redirect(Request.Url.ToString());
myConnection.Close();
}
catch (Exception)
{
Response.Write("<h2>ERROR</h2>");
}
Share
Improve this question
edited Apr 19, 2011 at 7:49
Soner Gönül
asked Apr 19, 2011 at 7:29
Soner GönülSoner Gönül
99k103 gold badges222 silver badges373 bronze badges
7
- what are you really trying to do? Client validation? there are much better methods. Server validation? you normally send the error message back to your view / aspx page and show it – balexandre Commented Apr 19, 2011 at 7:31
- What do you mean by "without postback"? What should trigger the alert box? – Heinzi Commented Apr 19, 2011 at 7:33
- Server validation. I want to use it with my database connection try-catch blocke.. – Soner Gönül Commented Apr 19, 2011 at 7:34
- But if you want to do server validation, you need to run code on the server, right? That means you have to transfer control back to the server, i.e. you need either a postback (which you apparently don't want to use) or AJAX. Do you use AJAX? – Heinzi Commented Apr 19, 2011 at 7:36
- @Heinzi I update my question. Please look at. – Soner Gönül Commented Apr 19, 2011 at 7:49
4 Answers
Reset to default 3See a note from MSDN:
If you want to register a script block that does not pertain to partial-page updates, and if you want to register the script block only one time during initial page rendering, use the
RegisterClientScriptBlock
method of theClientScriptManager
class. You can get a reference to theClientScriptManager
object from theClientScript
property of the page.
So, I think ClientScriptManager.RegisterStartupScript
method is what you need:
ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(
this.GetType(),
" ",
@"<script language=javascript>alert('ERROR');</script>",
true
);
In your code you forgot the quotation mark. I just tried it in a sample page like this:
Response.Write("<script language=javascript>alert('ERROR');</script>");
and it worked. Where did you place the Response.Write in your code? Could you give some more details? What do you want to do?
For displaying an alert message to the User, in a webpage i have a code have a look at this
public void UserMsgBox(string sMsg)
{
StringBuilder sb = new StringBuilder();
System.Web.UI.Control oFormObject = null;
sMsg = sMsg.Replace("'", "\\'");
sMsg = sMsg.Replace(Strings.Chr(34), "\\" + Strings.Chr(34));
sMsg = sMsg.Replace(Constants.vbCrLf, "\\n");
sMsg = "<script language='javascript'>alert(\"" + sMsg + "\")</script>";
sb = new StringBuilder();
sb.Append(sMsg);
foreach (System.Web.UI.Control oFormObject_loopVariable in this.Controls) {
oFormObject = oFormObject_loopVariable;
if (oFormObject is HtmlForm) {
break; // TODO: might not be correct. Was : Exit For
}
}
oFormObject.Controls.AddAt(oFormObject.Controls.Count, new LiteralControl(sb.ToString()));
}
try using RegisterStartupscript for registering the script. Refer : http://msdn.microsoft./en-us/library/system.web.ui.page.registerstartupscript.aspx