Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegisterStartupScript doesn't work with ScriptManager,Updatepanel. Why is that?

protected void timer1_Tick(object sender, EventArgs e)     {         foreach (RepeaterItem item in rpChat.Items)         {             TextBox txt = item.FindControl("txtChatMessage") as TextBox;             if (txt != null)             {                 message[i] = txt.Text;                 i--;             }         }         lblStatusChat.Text = "";         RepeaterBind();         string javaScript = "<script language=JavaScript>\n" + "alert('Button1_Click client-side');\n" + "</script>";          Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", javaScript);     } 

timer_click trigggers and update panel. And the alert message doesnt show up on timer_tick event

like image 778
sly_Chandan Avatar asked Jul 25 '12 05:07

sly_Chandan


People also ask

How does ScriptManager RegisterStartupScript work?

The script block that is rendered by the RegisterStartupScript method executes when the page finishes loading but before the page's client onload event is raised. Startup script blocks are located at the bottom of the rendered ASP.NET page just before the </form> tag.

What is the use of ScriptManager RegisterStartupScript in asp net?

I have used ScriptManager. RegisterStartupScript() method in order to show an alert when particular thing happens in back end.It works fine in page load method but not in particular method which is called when a specific button is clicked .


Video Answer


1 Answers

When you use an UpdatePanel, then you can not call JavaScript using ClientScript as you have tried to. You have to use ScriptManager.RegisterStartupScript instead.

So change your

Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", javaScript); 

to

ScriptManager.RegisterStartupScript(updatePanelId,updatePanelId.GetType(), "alert", javaScript, true); 
like image 59
Akash KC Avatar answered Oct 04 '22 20:10

Akash KC