Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

I am developing website in asp.net using 4.0 version. For this i am using Web user control and simple aspx page and i am using CSS,Javascript in web User Control after using this when i add in .aspx page after checking source code shows in body part instead of Head Part. So Please tell me how can i use CSS which is using in web user control show in Head Part.
Posted
Updated 17-Aug-15 2:53am
v2

1 solution

There are a couple of ways to do this, but it isn't very necessary.

I have controls that may appear more than once on the page (custom textboxes for eg). If I include the script every time I include the control then things go wrong. I instead use the ScriptManager control to manage these for me.
C#
public partial class PassengerManagement : System.Web.UI.UserControl
{
  private const string PassengerManagementJsName = "CustomPassengerManagementJS";
  private const string PassengerManagementJsUrl = "/Resources/js/custom.passengerMgmt.js";

  protected override void OnInit(EventArgs e)
  {
    base.OnInit(e);

    Type cstype = Page.GetType();

    ClientScriptManager cs = Page.ClientScript;

    if (!cs.IsClientScriptIncludeRegistered(cstype, PassengerManagementJsName))
      cs.RegisterClientScriptInclude(cstype, PassengerManagementJsName, PassengerManagementJsUrl);
  }
}


This ensures that the script is included once and only once. I can do the same with IsClientScriptBlockRegistered in the same way.

The script manager will be the first control within the form, and that is where the javascript includes and blocks will appear.

As for the css, You should design it in such a way that makes every class, control, named control style are unique so include all css in the Master page header. Either this or include a second ContentPlaceholder in the Master page head so that child pages can add generic css to the page head. Doing this for controls javascript really isn't needed.

I hope that helps ^_^
Andy
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900