开发者

Adding jQuery to Master Pages

I have the following jquery in my ASP.NET regular pages

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Color Items</title>
        <script src="http://code.jquery.com/jquery-latest.js"     
            type="text/javascript"></script>
        <script type="text/javascript">
            $(function() {
                $("select option:even").addClass('evenColor');
            });
        </script>
        <style type="text/css">
        .evenColor
        {
            background-color:Gray;
        }
        </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem Text="Gori"></asp:ListItem>
            <asp:ListItem Text="Muf"></asp:ListItem>
            <asp:ListItem Text="Lizo"></asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Now i wa开发者_开发技巧nt to convert this into a Master Pages. I am unable to figure out what will be added to the master page and what to the content page. Will the scripts go to MasterPage?

Please Note: I have many such regular asp.net pages with individual jquery files. Will I have to add all these jquery files into one master page.

What is the correct way?


Definitely add the reference to the common jquery files in your master page.

I would leave page specific javascript to that page, though others (especially if performance is of great importance) would argue to compile this as much as possible into one file, with just the specific commands on the page.


  1. Put a reference to the jQuery script file in your Master Page (you may decide to do this using a ScriptManager control if using any of MS AJAX library too).

  2. Put a reference to any page specific JavaScript file in that page only

  3. Refactor any page specific JavaScript code that repeats over multiple pages into one file that is referenced by the pages.


In addition to the suggestion to add the jQuery links in a master page, I'd suggest using nested master pages so that the base master page has markup, and the derived master includes the jQuery. That way you can choose to use the jQuery enabled master, or the non-jQuery (base) master page on a page-by-page basis for optimal performance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜