简体   繁体   中英

Javascript Alert from C# Class

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.

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

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