I'm using the method below as a button-click event on an ASP.NET 4.0 page. The first alert box (ClientScript.RegisterStartupScript(...)) works fine, the second I haven't tested yet; but the third doesn't produce an alert - and it doesn't produce an error either. Can anyone suggest what I'm missing here? I'm doing the same thing in all 3 attempts to display the alert box - using a string variable as the text to display.

From CodeBehind.aspx.cs:

protected void btnUpdateClients_Click(object sender, EventArgs e)
  {
    string[] strRecs;
    string strResponse = util.ValidateInput(this.FileUploadGuids.FileName, "none", this.txtBuild.Text, this.rbMTBFEnvs.Text);
    Stream myStream = FileUploadGuids.FileContent;
    int contentLen = (int) FileUploadGuids.PostedFile.ContentLength;
    if (strResponse.Length > 0)
    {
      ClientScript.RegisterStartupScript(this.GetType(), "MyKey", "alert('" + strResponse + "');", true);
      return;
    }
    strResponse = "";
    strRecs = util.BuildStringArray(myStream, contentLen);
    string[] theCreds = util.getCreds(CredsDict, this.rbMTBFEnvs.Text).Split(',');
    try
    {
      PrincipalManagement pmClass = new PrincipalManagement(theCreds[0], theCreds[2], theCreds[3], theCreds[1]);
      strResponse = util.ClientUpdates(pmClass, strRecs, this.txtBuild.Text);
    }
    catch (Exception ex)
    {
      ClientScript.RegisterStartupScript(this.GetType(), "MyKey", "alert('" + ex.Message + "');", true);
    }
    
    ClientScript.RegisterStartupScript(this.GetType(), "MyKey", "alert('" + strResponse + "');", true);
  }

Recommended Answers

All 5 Replies

I think you may have a problem with your RegisterStartupScrip, you could be using RegisterClientScriptBlock instead.

Hi Fortinbra,
I don't think I understand what you mean... can you elaborate?

Register startup script is intended to be used for a script you want executed as soon as the page is loaded, or during page load. register client scrip block is for registering a JavaScript block that can be called by controls during the page execution.

After looking through your code a little closer, it looks like your trying to do the former in all three cases. If you are trying to do this on postback you might want to make sure that your identifier on the client side isn't already used. Trying using the IsClientScriptBlockRegistered method to check to make sure that the unique identifier isn't already there.

Thanks Fortinbra. I understand what you're explaining and it seems valid. However, the last Alert (the one I'm having trouble with) executes correctly if I replace the strResponse string object with a simple quoted string; e.g. 'Response'.
What I have found is that the reason it is not working is because I have embedded '\r\n' escape sequences in the strResponse string. I've found that if I try to display the Alert even with just the quoted string, it will not work if it has escape sequences in it:
'Response' - works
'Response\r\n', 'Response\r', 'Response\n' - do NOT work
I'll close this thread as solved and do some more research. If I cannot find an answer or relevant work-around, I'll open another fresh thread.
Thanks for your help!

Just a final post to this thread - to actually close it out.
I researched the escaping problem I was seeing and found that if I build the string object (strResponse) using '\\n' for the line breaks, instead of '\r\n', the alert works as expected.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.