18

The included script references, in particular jQuery, are being rendered after viewstate. Is there a way to get this in the < head>?

Page.ClientScript.RegisterClientScriptInclude("jQuery", "/scripts/jquery.js");

I am trying to register jquery.js in a user control's page load.

Thanks in advance!

P.S. If it can't be done (with ClientScript), anyone have an idea why they didn't build it in?

UPDATE

The main feature of the ClientScript manager I need is the ability to only include a script once. The control can appear many times on a page, but i only want one jQuery script include

4 Answers 4

24

to directly inlcude it in the HEAD:

HtmlGenericControl Include = new HtmlGenericControl("script"); 
Include.Attributes.Add("type", "text/javascript"); 
Include.Attributes.Add("src", sInclude); 
this.Page.Header.Controls.Add(Include); 

you would want to check to make sure its not there already before adding it.

3
  • Another perfectly valid method, bypassing the PlaceHolder that I used. Commented Feb 1, 2009 at 2:36
  • Thank you Glennular, that check for uniqueness seems to be the useful bit of ClientScript
    – ccook
    Commented Feb 1, 2009 at 2:40
  • After struggling a lot. This is the perfect answer. Commented Feb 26, 2014 at 12:25
9

I had this problem a while back, and I ended up not using RegisterClientScriptInclude.

I placed a placeholder in the header of the page, and added the script tag to the placeholder via a HtmlGenericControl.

I'll see if I can find my code and I'll edit my answer with it.

EDIT

I couldn't find my code, so I just re-created it:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <asp:PlaceHolder runat="server" ID="HeadPlaceHolder"></asp:PlaceHolder>
</head>    
<body>
    ...
</body>
</html>

And the Code Behind:

protected void Page_Load(object sender, EventArgs e)
{
    HeadPlaceHolder.Controls.Add(/* Your control here */);
}
2
  • Thank you! Yea, everything I'm reading out there indicates it will not render into the head.
    – ccook
    Commented Feb 1, 2009 at 2:29
  • This was perfect - the placeholder is useful to locate the scripts exactly where you want in the head, mainly just for neatness. I also used a separate one for stylesheets. The Page.Header.Controls.Add approach only appends to the end, so everything tends to get intermingled. With PlaceHolder, the scripts are grouped; stylesheets are grouped.
    – goodeye
    Commented Jul 6, 2011 at 22:04
3

Hey, old question, but maybe this is still of interest for someone.

I am creating a own UserControl with .net 3.5sp1, ran into the same problems. Following solution works for me.

This code is from the UserControl class:

protected void Page_Init( object sender, EventArgs e )
{
    const string scriptKey = "UserControlScript";

    if( !Page.ClientScript.IsClientScriptIncludeRegistered( Page.GetType(), scriptKey ) )
    {
        Page.ClientScript.RegisterClientScriptInclude( Page.GetType(), scriptKey, ResolveClientUrl("~/js/UserControl.js" ) );
    }
}

I used Page_Init because I need to do some more initialization that has to be done before Page_Load of the nesting page is called.

1

It appears its not possible to use Page.ClientScript to add scripts to the header of the page.

Not the answer you're looking for? Browse other questions tagged or ask your own question.