I have the following code, but the alert box is not displaying.
try
{
do something..
}
catch(Exception ex)
{
Response.Write("<script>alert('"+ex+"')</script>");
}
If I use this code, the alert box appears.
try
{
do some thing
}
catch (Exception ex)
{
Response.Write("<script>alert(\"an error occur\")</script>");
}
How can I display the exception variable in an alert box?
If you want to show the stacktrace:
Response.Write("<script>alert('"+ Server.HtmlEncode(ex.ToString()) + "')</script>");
or if you want only the message
Response.Write("<script>alert('"+ Server.HtmlEncode(ex.Message) + "')</script>");
Try something like
Response.Write("<script>alert('"+ex.Message+"')</script>");
Have a look at the class Exception Class
Dim message = New JavaScriptSerializer().Serialize(rs)
Dim script = String.Format("alert({0});", message)
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "", Script, True)
This solved my problem:
string jscriptCustInfo = "<script type='text/javascript' language='javascript'>";
jscriptCustInfo = jscriptCustInfo + "alert('Dividend Posting Done, Batch No: "+lblBatch.Text+"');";
jscriptCustInfo = jscriptCustInfo + "</script>";
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", jscriptCustInfo, false);
请检查您在该页面中使用更新面板的情况。如果有更新面板,有时可能会有效。
You need to be careful and properly escape the Javascript string you are generating ... Imagine there are single quotes in the Exception's message ...
Single-quotes ( '
) need to be escaped ( \\'
)
Response.Write("<script>alert('"+ Server.HtmlEncode(ex.Message).Replace("'","\\'" ) + "')</script>");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.