简体   繁体   中英

How do I display an alert message on my ASP .NET page?

This is not working:

try
{
     EnvironmentVerifier.VerifyAppFoldersAndFiles();
}
catch (Exception ex)
{
     ClientScript.RegisterStartupScript(GetType(), "Error!", "alert('" + ex.Message + "');", true);
     Logger.LogError(ex.Source, ex.TargetSite.ToString(), ex.Message);
     return;
}

When the error occurs, it goes into the catch block but the alert message is not showing up. Am I missing anything?

Try this:

ClientScript.RegisterStartupScript(GetType(), "Error!", "alert('" + ex.Message.Replace("'", @"\'") + "');", true);

The .Replace("'", @"\\'") escapes your alert('message'); because if you have an error message like this:

alert('My error message's problem is that single quote.');

it will break unless you do this:

alert('My error message\'s problem is that single quote.');

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM