0

Here is my code where I register a script block to show a Javascript alert from my C# class:

public static void MessageBox(string strMessage)
{
    // Gets the executing web page
    Page page = HttpContext.Current.CurrentHandler as Page;

    string script = string.Format("alert('{0}');", strMessage);

    // Only show the alert if it's not already added to the 
    if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
    {
        page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script, true /* addScriptTags */);
    }
}

This works great when I call the MessageBox function before the DOM has been fully loaded. However, when I call this function dynamically (ex: if a user hit submit and an error was caught) after the DOM has been fully loaded, the alert does NOT pop up.

Is there a reason why my initial call before the DOM is fully loaded works, while the same call made after the DOM was loaded does not work?

EDIT:

After checking out the link below in the comments, I gave it a shot:

Page page = HttpContext.Current.CurrentHandler as Page;
ScriptManager.RegisterClientScriptBlock(page, typeof(Page), "MyScript", "alert('heyyyyyy');", true);

Although ScriptManager is supposed to be for handling AJAX calls, this produces the SAME result as my original attempt above. The Javascript alert pops up on the initial page load, but never pops again (on any of my AJAX requests).

EDIT (2):

Here is my MessageBox function:

public static void MessageBox(string strMessage)
{
    Page page = HttpContext.Current.CurrentHandler as Page;
    ScriptManager.RegisterClientScriptBlock(page, typeof(Page), "MyScript", "alert('heyyyyyy');", true);
}

Here is one place I call it:

public static string GetLoggedOnUserId(string strLogonUser)
    {
        string strUserId = string.Empty;

        try
        {
            MessageBox("hey");
            int intPositionOfSlash = strLogonUser.IndexOf("\\");
            strUserId = strLogonUser.Substring(intPositionOfSlash + 1, 6).ToUpper();
        }
        catch (Exception ex)
        {
            ErrorHandler('B', ex);
        }

        strLoggedOnUser = strUserId;
        return strUserId;
    }

And this call will make the alert pop up (since it's on the first page load).

Here is the second time I attempt to call the MessageBox function:

public static string LoadListItems(string strListItemStoredProcedureName, string strConnectionString)
{
    try {
        string strSQLQuery = " EXE " + strListItemStoredProcedureName.Trim() + " ";
        SqlCommand objCommand = new SqlCommand(strSQLQuery, objConnection);
        // other code here...
    }
    catch (Exception ex) {
        MessageBox("error");
    }
}

This call does NOT pop up the alert... Keep in mind, this function is called via an AJAX post - NOT on a page reload.

12
  • What do you mean by the DOM is fully loaded? As far as I remember all client scripts go to one place in the HTML document and it does not matter when during the page life-cycle you add them (well almost :)). Are there any JavaScript errors? Commented Nov 4, 2013 at 19:01
  • There are no Javascript errors... And what I mean by "DOM is fully-loaded" is: once all HTML/Javascript has finished loading in my browser. Commented Nov 4, 2013 at 19:18
  • I think I may have found a (or THE) reason. Seems like RegisterClientScriptBlock only fires on startup and on subsequent page postbacks. Commented Nov 4, 2013 at 19:29
  • So you are using AJAX :) Then look at that post stackoverflow.com/a/297291/1197333 Commented Nov 4, 2013 at 19:32
  • Yes, I took a look at that link and gave it a shot (see my EDITs) but it still seems to be working the exact same way as my original attempt. It shows the Javascript alert on the initial page load, but does NOT show whenever I make AJAX calls... Commented Nov 4, 2013 at 20:00

1 Answer 1

1

I ended up using a class I found here: http://www.c-sharpcorner.com/uploadfile/mahesh/webmsgbox09082006110929am/webmsgbox.aspx

This works no matter if I'm on page load, unload, AJAX call, etc. Simple class that you call using:

WebMsgBox.Show("Your message here");

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.