Tag Archive for 'updatepanel'

ASP.NET Ajax, UpdatePanels and ClientScriptBlocks

For the project I am currently doing, I’m helping in developing a web application. The application itself is built on ASP.NET Ajax and thus uses UpdatePanels. One of the advantages of an UpdatePanel is that you can use the ScriptManager to execute Javascript on the client side. Today, a coworker attended me to the fact that the ScriptManager.RegisterClientScriptBlock was not working from his code behind. The code was built as a user control (.ascx), containing an UpdatePanel from which he executed his code. The control itself was loaded dynamically onto the page. The code simply did not execute, and no errors or exceptions were thrown either.

After some searching (Google, Bing, you choose), the solution turned out to be a simple one: he called the RegisterClientScriptBlock-method specifying the UpdatePanel as control to register on, which is IntelliSense’s default suggestion. After reading through MSDN, I found out that an overloaded method is available which accepts a page.

What didn’t work:
ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, typeof(UpdatePanel) Guid.NewGuid().ToString(), myScript, true);

What worked:
ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel) Guid.NewGuid().ToString(), myScript, true);