Sunday, January 18, 2009

Page.RegisterStartupScript not firing properly in Ajax Extensions


Page.RegisterStartupScript not firing properly in Ajax Extensions
.code {
word-wrap:break-word;
margin:10px;
padding:10px;
border:2px ridge white;
background-color:#eeeeee;
font-family:Courier New;
font-size:10pt;
}

Recently, we were working on a AJAX Enabled site, and there was a functionality to fire a script in the page load event of a certain page. This page had ajax relevant controls in the .aspx page, along with the Update panel and ScriptManager Control.
We coded the below given method in the .aspx.cs page, for firing the script call:
Page.RegisterStartupScript("strScript5", strScript5);This code is supposed to fire the clientscript in the startup of the page, but it didn't. Something was wrong somewhere, since we had always trusted this method for the script method calls. We then tried using the following method:
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "strScript5", strScript5);
Even this was not of any use. Then we started to check the MSDN and ASP.NET Forums for answer....
The actual problem was that since we used the ScriptManager and other AJAX Controls like Update panel, the firing of page event were only partial callbacks, so the code that otherwise would execute properly, didn't execute.We later came to the solution given below
ScriptManager.RegisterStartupScript(UP, UP.GetType(), strGridKey, strScript4, false);
Explanation :
Registers a startup script block for every asynchronous postback with the ScriptManager control and adds the script block to the page.
public static void RegisterStartupScript( Page page, Type type, string key, string script, bool addScriptTags)

No comments:

Post a Comment