Wednesday, November 24, 2010

ScriptManager.RegisterStartupScript() vs. ClientScript.RegisterStartupScript()


I have been struggling in manipulating scripts to focus to a particular control when a user executes a button or events triggered by postbacks.

The scenario was in every button clicked, the focus will be transferred to another control. These butttons are inside an update panel and ajax tab container.

The solution presented in an article i've previously posted was using ClientScript.RegisterStartupScript() to register a script. However, this script is registered in the Page object. But since my controls are inside an update panel which is partial postback rendering, the solution I've come up was using ScriptManager.RegisterStartupScript().

So, to register client scripts on an aspx page w/o ajax functionalities, use ClientScript:
ClientScript.RegisterStartupScript(this.GetType(), "MyScript", script.ToString(), false);  

And to register client scripts on an aspx page w/ ajax functionalities such as inside an update panel, use ScriptManager:
ScriptManager.RegisterStartupScript(this, this.GetType(), "MyScript", script.ToString(), false);  


Post a Comment