2

I’m trying to call a JavaScript function from Code behind but no luck so far. I tried to add the following snippets inside Page_Load method.

I’ve tried as below

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "foo", "alert('test22222')", true);

Also as below

Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "alert('test22222');", true);

None worked for me. Is there anything I’m missing here? I would like to show the alert message before loading the page.

Any help appreciated. Thanks.

MJM
  • 89
  • 1
  • 7

3 Answers3

2

You are missing a ; in you code. Try this it worked for me.

But i would suggest the ScriptManager.RegisterStartupScript over the Page.ClientScript.RegisterStartupScript as the first one is designed for AJAX calls. Which will work for even partial page post backs.

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "foo", "alert('test22222');", true);
Guru Kara
  • 6,272
  • 3
  • 39
  • 50
1

you can implement it in page_prerender event

    protected void page_prerender( object sender, EventArgs e )
{
         your code here;
}
  • This behaves same as the current one :( – MJM Apr 17 '13 at 09:51
  • I want to call a function before the page renders in the screen. So I tried to implement it. It didn't get called. I even called alert as given in the code that also didn't work. – MJM Apr 17 '13 at 09:57
0

This will work. You forgot semicolon at end of your alert:

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "foo", "alert('test22222');", true);
Yeronimo
  • 1,731
  • 15
  • 28