How to include jQuery in ASP.net page?

前端 未结 4 928
無奈伤痛
無奈伤痛 2021-01-02 01:12

i have an ASP.net UserControl that requires the containing page to include a reference to jquery.

In the olden days, i would simply have included a refe

相关标签:
4条回答
  • 2021-01-02 01:45

    You can use a ScriptManagerProxy on the UserControl and a ScriptManager on the parent or master page.

    See How Do You Use ScriptManagerProxy In a custom ASP.NET Control

    This would take care of "Giving RegisterXxxxScriptXxx the path to "Scripts/jquery-1.7.2.min.js" and remove the need to worry about it during the Page_Load/Page_PreRender events.

    As for "When would i want to use RegisterClientScriptInclude vs RegisterStartupScript?"

    RegisterClientScriptInclude registers an external JS file to be included in the page. RegisterStartupScript includes a block of inline executable script in the page, which is not in an external file.

    0 讨论(0)
  • 2021-01-02 01:53

    You can use google hosted jquery as follows:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
    
    0 讨论(0)
  • 2021-01-02 01:55

    [this is only relevant if your user control is to be used in-house. If it's for distribution then it won't be of much help]

    Take a look at this link:

    http://www.codeproject.com/Articles/196727/Managing-Your-JavaScript-Library-in-ASP-NET

    The article suggests creating methods to generate references to javascript libraries such as jQuery, so that if you want to use it in a page you simply call JavascriptLoader.IncludeJQuery() [or whatever you have called your method].

    Now what I have done is to take it a step further by creating those methods in an assembly that I have placed in the GAC so that it is available to all my .net web applications. Now, wherever I want to use jQuery, that method is already available. The best thing being that if I call the method in a user control, and call it again in another user control, and again on the page, it still only registers the library once. If I decide to upgrade to a newer version of jQuery, I just change my dll, and it's changed everywhere.

    0 讨论(0)
  • 2021-01-02 01:57

    If your control is referenced from a separate project, you can embed the javascript in the other assembly. See this old but good example by Scott Mitchell:

    http://www.4guysfromrolla.com/articles/080906-1.aspx

    0 讨论(0)
提交回复
热议问题