Wednesday, May 02, 2012

solution to: c# asp.net usercontrol javascript only executes first time

I have this line to run a javascript from code behind:
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "sbScript", sbScript.ToString(), false);

This line is inside pageload event of the usercontrol that I used my my aspx page. The user control is used multiple times on the aspx page. The javascript is therefore called multiple times. However the script only works the first time.

The reason being is because the script name specified is always "sbScript" which has been assigned value when first run. The solution is to make sure the script name is set to different values everytime the line is executed.

No comments: