简体   繁体   中英

asp.net javascript message not showing up

I have a button and onclick is set to this method which should display a simple JS alert pop up window:

string message = "File is already open. <br>Please close the file and try again.";
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("<script type = 'text/javascript'>");
    sb.Append("window.onload=function(){");
    sb.Append("alert('");
    sb.Append(message);
    sb.Append("')};");
    sb.Append("</script>");
    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());

I have used the code above before and it has worked but not this time. The only difference is that I was using master pages for the other site and not for this one, and this one also has a timer going.

Is there anything JS related to load in the <head> of the aspx page?

protected void btnToCSV_Click(object sender, EventArgs e)
    {
        try
        {
            StreamWriter writer = new StreamWriter(@"\\server location\test.csv");

            Some writer.write stuff...

            writer.Close();
        }
        catch (Exception ex)
        {
            lblMessage.Visible = true;
            string message = "File is already open. Please close the file and try again.";
            ClientScript.RegisterClientScriptBlock(
               this.GetType(),
              "alert",
              string.Format("alert('{0}');", message),
              true);

        }
}

Try with this simplyfied version

string message = "File is already open. <br>Please close the file and try again.";
ScriptManager.RegisterClientScriptBlock(
  UpdatePanel1, // replace UpdatePanel1 by your UpdatePanel id
  UpdatePanel1.GetType(), // replace UpdatePanel1 by your UpdatePanel id
  "alert", 
  string.Format("alert('{0}');",message), 
  true );

You are not encoding anything here:

sb.Append(message);

If the message is Hello Jean d'Arc (notice the single quote) you will end up with the following code:

alert('Hello Jean d'Arc');

I am leaving you imagine the result of this => a javascript error of course.

To fix this error make sure that you have properly encoded the argument. One way to do this is to JSON serialize it:

var serializer = new JavaScriptSerializer();
sb.AppendFormat("alert({0});", serializer.Serialize(message));

Also since you have already includeed the <script> tags, make sure that you pass false as last argument of the RegisterClientScriptBlock function to avoid adding them twice:

ClientScript.RegisterClientScriptBlock(
    this.GetType(), 
    "alert", 
    sb.ToString(), 
    false           // <!---- HERE
);

And here's how the full code might look like:

var message = "File is already open. <br>Please close the file and try again.";
var sb = new StringBuilder();
sb.Append("<script type=\"text/javascript\">");
sb.Append("window.onload=function() {");
var serializer = new JavaScriptSerializer();
sb.AppendFormat("alert({0});", serializer.Serialize(message));
sb.Append("};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(
    this.GetType(), 
    "alert", 
    sb.ToString(), 
    false
);

Another remark is that if you are doing this in an AJAX call you should use the ClientScript.RegisterStartupScript method.

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