Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript Alert from C# Class
    primarykey
    data
    text
    <p>Here is my code where I register a script block to show a Javascript alert from my C# class:</p> <pre><code>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 &amp;&amp; !page.ClientScript.IsClientScriptBlockRegistered("alert")) { page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script, true /* addScriptTags */); } } </code></pre> <p>This works great when I call the <code>MessageBox</code> function <strong><em>before</em></strong> the DOM has been fully loaded. However, when I call this function dynamically (ex: if a user hit submit and an error was caught) <strong><em>after</em></strong> the DOM has been fully loaded, the alert does NOT pop up.</p> <p>Is there a reason why my initial call <strong><em>before</em></strong> the DOM is fully loaded works, while the same call made <strong><em>after</em></strong> the DOM was loaded does not work?</p> <p><em><strong>EDIT:</em></strong></p> <p>After checking out the link below in the comments, I gave it a shot:</p> <pre><code>Page page = HttpContext.Current.CurrentHandler as Page; ScriptManager.RegisterClientScriptBlock(page, typeof(Page), "MyScript", "alert('heyyyyyy');", true); </code></pre> <p>Although <code>ScriptManager</code> 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).</p> <p><em><strong>EDIT (2):</em></strong></p> <p>Here is my <code>MessageBox</code> function:</p> <pre><code>public static void MessageBox(string strMessage) { Page page = HttpContext.Current.CurrentHandler as Page; ScriptManager.RegisterClientScriptBlock(page, typeof(Page), "MyScript", "alert('heyyyyyy');", true); } </code></pre> <p>Here is one place I call it:</p> <pre><code>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; } </code></pre> <p>And this call will make the alert pop up (since it's on the first page load).</p> <p>Here is the second time I attempt to call the <code>MessageBox</code> function:</p> <pre><code>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"); } } </code></pre> <p>This call does <strong><em>NOT</em></strong> pop up the alert... Keep in mind, this function is called via an AJAX post - NOT on a page reload.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload