views:

176

answers:

3

The "Header control" we use holds a jquery reference. When I attempt to leverage any jquery functionality from the Master page it fails because the jquery reference has not been added yet.

Is there a way to force the Header Control that is embedded into the Master Page to load it's resources before the Master Page attempts to reference them?

A: 

You can use the ScriptManager in the template master to register your javascript inclue file for JQuery.

You can use

  • ScriptManager.RegisterStartupScript or
  • ScriptManager.RegisterClientScriptInclude

Another way is in the template master, in the oninit method use this code

Page.Header.Controls.Add(new LiteralControl(" <script src="jquery" type="text/javascript"></script> "));
Cédric Boivin
A: 

You could simply move the JQuery script reference into the aspx of the master page itself (at the top, of course). Then, any controls would be able to access JQuery.

John Fisher
+1  A: 

I'd include the jquery reference in the head of the master page itself.

Or if you don't want the jquery on every page then you can do this in your master page:

<head runat="server">
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder> 
</head>

And then do this on the aspx page that needs jquery:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
</asp:Content>

Either way you do it accomplishes the same thing. It includes the jquery file in the "head" of your HTML. And that's the easiest way to ensure jquery works properly.

Steve Wortham