Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

master and content pages and jquery

Tags:

jquery

asp.net

I want to use jQuery in my ASP.NET 3.5 website that uses master pages and content pages. How do I do document ready() functions for the child pages for those that will use jQuery ? Where do I put the code?

I figured the jQuery declarations should go in the master page, but don't know how to make sure any jQuery calls go into the HEAD of the resolved page.

like image 210
Caveatrob Avatar asked Jan 20 '23 15:01

Caveatrob


2 Answers

Just put a content placeholder in the master's head section, and then write page-specific scripts withing the asp:Content tags of your pages.

Master

<%@ Master Language="C#" %>
<html>
    <head>
        <script type="text/javascript" src="scripts/jquery-1.4.3.js"></script>
        <asp:contentplaceholder id="LocalScripts" runat="server" />
    </head>
    <body>
    </body>
</html>

Page

<%@ Page Language="C#" MasterPageFile="~/Master1.master" 
         AutoEventWireup="true" Title="MyTitle"  %>
<asp:Content ID="Content1" Runat="Server" ContentPlaceHolderID="LocalScripts" >
    <script type="text/javascript"> 
        $(document).ready(function () {
           // some local script logic here
        });
    </script>
</asp:Content>
like image 174
Mike Marshall Avatar answered Jan 29 '23 23:01

Mike Marshall


You should be able to just use something like this:

In child page (I don't think it matters greatly WHERE it is put in the page):

<script type="text/javascript">
    $(document).ready(function () {
        //Do work here
    });
</script>

as long as you have included the necessary jQuery files (libraries and plug-ins) in your master page.

like image 34
Rion Williams Avatar answered Jan 29 '23 22:01

Rion Williams