0

I've got this code from Mads Kristensen's blog to display a message box on an ASP.NET client's page using Javascript:

public static class Alert
{
    public static void Show(string message)
    {
        // Cleans the message to allow single quotation marks 
        string cleanMessage = message.Replace("'", "\\'");
        string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";

        // Gets the executing web page 
        Page page = (Page)HttpContext.Current.CurrentHandler;

        // Checks if the handler is a Page and that the script isn't allready on the Page 
        if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
        {
            page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script, false);
        }
    }
}

And it works fine when there is no master page involved. However, when I call it (on the server side) from a master page derived page it doesn't work. I'm guessing it is something related to the RegisterClientScriptBlock() method, but I don't know what. Can someone show me the way to solve this?

2
  • What's the actual error that you get?
    – Greg
    Commented Feb 29, 2012 at 3:11
  • Hi Greg, didn't notice your comment here. Actually no error message is shown, the "alert" pop up box simply doesn't show up when the "Show" method is called in a child page, but everything works when it is called in a master page. Commented Mar 11, 2012 at 14:17

1 Answer 1

1

Just in case anyone stumbles on this, I found out a workaround to my issue. It happens that my master page has its ContentPlaceHolder tag inside an UpdatePanel so when I replaced:

page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script, false);

By:

ScriptManager.RegisterStartupScript(page, page.GetType(), "alert", script, false);

The alert message box started showing up on all pages.

Not the answer you're looking for? Browse other questions tagged or ask your own question.