Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScriptManager.RegisterStartupScript code not working - why?

Tags:

I have used code like this in the past to successfully pop up an alert message on my asp.net webpage. Now it is not working. I can't figure out why.

ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID,       "alert('This pops up')", true); 

Any ideas?

like image 912
TheMoot Avatar asked Feb 14 '11 15:02

TheMoot


1 Answers

Off the top of my head:

  • Use GetType() instead of typeof(Page) in order to bind the script to your actual page class instead of the base class,
  • Pass a key constant instead of Page.UniqueID, which is not that meaningful since it's supposed to be used by named controls,
  • End your Javascript statement with a semicolon,
  • Register the script during the PreRender phase:

protected void Page_PreRender(object sender, EventArgs e) {     ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey",          "alert('This pops up');", true); } 
like image 68
Frédéric Hamidi Avatar answered Oct 07 '22 07:10

Frédéric Hamidi