What is the best way to programmatically run javascript when an ASP.net page loads? - Stack Overflow

In my global.asax file for my ASP project, I am checking for certain conditions.When those conditions

In my global.asax file for my ASP project, I am checking for certain conditions. When those conditions are met, I want to automatically execute javascript code when the page runs.

This is my code:

if condition Then
Response.Write(" < script type=""text/javascript"" > ")
Response.Write(" // Javascript code to do stuff ")
Response.Write(" < /script > ")
End If

While this appears to work to execute the Javascript code, I don't think it's a best practice because this code will preceed all of the HTML of the page that gets loaded.

What is the best way of programmatically tacking on some extra Javascript code to be run when my page loads?

Update Thanks for the answer. Too bad this solution doesn't work from within global.asax. Is there no way to make this happen site-wide? It seems like global.asax would be the logical place to put code that runs with every page... Response.Write works fine in global.asax.

In my global.asax file for my ASP project, I am checking for certain conditions. When those conditions are met, I want to automatically execute javascript code when the page runs.

This is my code:

if condition Then
Response.Write(" < script type=""text/javascript"" > ")
Response.Write(" // Javascript code to do stuff ")
Response.Write(" < /script > ")
End If

While this appears to work to execute the Javascript code, I don't think it's a best practice because this code will preceed all of the HTML of the page that gets loaded.

What is the best way of programmatically tacking on some extra Javascript code to be run when my page loads?

Update Thanks for the answer. Too bad this solution doesn't work from within global.asax. Is there no way to make this happen site-wide? It seems like global.asax would be the logical place to put code that runs with every page... Response.Write works fine in global.asax.

Share edited Jun 3, 2010 at 18:19 Vivian River asked Jun 3, 2010 at 17:56 Vivian RiverVivian River 32.5k64 gold badges210 silver badges324 bronze badges 1
  • I see that SO did some weird things to my code. I added some spaces to make it display properly. – Vivian River Commented Jun 3, 2010 at 18:21
Add a ment  | 

3 Answers 3

Reset to default 7

To correctly manage the placement of scripts from server controls or pages, you should use ClientScriptManager.RegisterStartupScript()

You can also use the equivalent method in the ScriptManager if you are using ajax controls in your site: ScriptManager.RegisterStartupScript

These methods take care of outputting your script in the appropriate locations to be run when the document is finished loading. The links posted have some good examples to work from.

Note that you won't be able to use these methods directly from global.asax, as it has no reference to the current page. Global.asax is for hooking events to the HttpApplication pipeline, and is decoupled from the actual HttpHandler such as your Page object. To keep the separation of UI, you'd be better off to do the check in your master page or in some base page class, and output the script from there.

back in the days of asp ajax 1.0 we had something called Sys.WebForms.PageRequestManager in the ajax client libraries. Haven't worked with asp 3.5 and asp ajax, but I am sure it is simply enough to add the asp ajax javascript file into your page and you'll be able to make use of it. Check out for an event called pageLoaded event.

ps: this event will fire every time there is a sync or async postback

If you want to do it on every page, put it on your masterpage if you have one.

If not, you can create a base Page class, that inherits from System.Web.UI.Page and extend it to check your conditions & render javascript.

Then in the codebehind of each page, inherit your base Page Class instead of System.Web.UI.Page

public partial class Orders : MyCode.BasePage

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745098107a4611105.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信