Proper way to use JQuery when using MasterPages in ASP.NET?

前端 未结 4 1107
日久生厌
日久生厌 2020-11-30 00:46

I have no issues when doing using JQuery in a aspx page without a masterpage, but when I try to use it in pages that have a masterpage, it doesn\'t work, so I end up putting

4条回答
  •  猫巷女王i
    2020-11-30 01:12

    You would declare your main jQuery scripts within the master page, as you would normally:

    
      
      
      
      
    
    

    And then any page specific JS files could be loaded within the Content controls that reference the Head ContentPlaceholder.

    However, a better option would be to look into the ScriptManager and ScriptManagerProxy controls - these can provide you with a lot more control over the way your JS files are served to the client.

    So you would place a ScriptManager control in you master page, and add a reference to the jQuery core code in that:

    
      

    Then, in your page that requires some custom JS files, or a jQuery plugin, you can have:

    
      
          
            
          
      
    

    The ScriptManager allows you to do things like control where on the page scripts are rendered with LoadScriptsBeforeUI (or better yet, after by setting it to False).

提交回复
热议问题