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.

FastTrack
  • 8,810
  • 14
  • 57
  • 78
  • 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? – Ivancho Nov 04 '13 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. – FastTrack Nov 04 '13 at 19:18
  • I think I may have found a (or THE) [**reason**](http://www.codedigest.com/Articles/ASPNET/314_Multiple_Ways_to_Call_Javascript_Function_from_CodeBehind_in_ASPNet.aspx). Seems like `RegisterClientScriptBlock` only fires on startup and on subsequent page postbacks. – FastTrack Nov 04 '13 at 19:29
  • So you are using AJAX :) Then look at that post http://stackoverflow.com/a/297291/1197333 – Ivancho Nov 04 '13 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... – FastTrack Nov 04 '13 at 20:00
  • can you define what browser returns you. you can use httpfox in mozilla and check the request header for the post/get and check the call? – Chandan Kumar Nov 04 '13 at 20:01
  • Maybe you should also Look at signalr – daniel Nov 04 '13 at 20:08
  • ensure about the parameters you are assigning. With AJAX enabled pages, you should use the ScriptManager to register scripts: ScriptManager.RegisterClientScriptBlock(Page, typeof(MyPage), "MyScript", "GoStuff()", true) – Chandan Kumar Nov 04 '13 at 20:08
  • Have a look at my **EDITS (2)** section for all my code... – FastTrack Nov 04 '13 at 20:10
  • yes but are you defining it in proper manner. Again i repeat, please check you parameters. from MSDN Definition : public void RegisterClientScriptBlock( Type type, string key, string script, bool addScriptTags ) – Chandan Kumar Nov 04 '13 at 20:11
  • or public void RegisterClientScriptBlock( Type type, string key, string script ) – Chandan Kumar Nov 04 '13 at 20:13
  • Yes, I am defining it correctly. The alert does pop up on page load, but NOT on subsequent AJAX requests... – FastTrack Nov 04 '13 at 20:26

1 Answers1

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");

FastTrack
  • 8,810
  • 14
  • 57
  • 78