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
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.
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 */);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With