While calling a JavaScript function from a Content Page Load method, you can use the ClientScript.RegisterStartupScript method. However the same does not work if the content page is wrapped inside an ASP.NET AJAX Update Panel. To call a Javascript function at page load in an Update Panel scenario, you need to instead use the ScriptManager.RegisterStartupScript method that registers a startup script block with the ScriptManager control and adds the script block to the page.
Here’s an example. In the Page_Load of the content page, write the following code:
This is the code that registers a script block
ScriptManager.RegisterStartupScript(this, this.GetType(), "call", str.ToString(), false);
Using the RegisterStartupScript(Page, Type, String, String, Boolean) overload registers a startup script block every time that an asynchronous postback occurs.
Update: On the same note, check out a nice article by Krishna Using .NET’s Register Script methods with jQuery’s $(document).ready()
Update: On the same note, check out a nice article by Krishna Using .NET’s Register Script methods with jQuery’s $(document).ready()
Tweet
4 comments:
It is recommended to use ToolkitScriptManager instead of ScriptManager in ASP.net 4.0 Ajax toolkit.
Sandun: Thanks for bringing up the ToolScriptManager. One of the main reasons why you would want to use the ToolScriptManager (which by the way inherits from ScriptManager) is that it reduces the number of ScriptResource.axd calls, by combining 'relevant' scripts and reducing n/w traffic.
Depending on a request, it's smart to generate a combined script file which has only those scripts that the browser has not yet downloaded.
very cool & good tips, thank you very much for sharing.
Can I use JavaScript function using this way????
Post a Comment