views:

212

answers:

2

Hi,

I am implementing a web application and i faced the following problem:

I have a User control with it's own styles and js files, when i add the user control to the page and i try to validate the murkup i get error due to the fact that the user control's styles and js are in the body of the page in stead of the head. i would like to expose the page head to add from my web application user controls the styles and js files directly into the page head. have you got any suggestion? i did not get any good solution online yet

thanks in advance

+1  A: 

Scott Gu wrote a post about this. You may try the following:

HtmlGenericControl script = new HtmlGenericControl("script");
script.Attributes.Add("type", "text/javascript");
script.Attributes.Add("src", "/somescript.js");
Page.Header.Controls.Add(script);
Darin Dimitrov
A: 

In the user control markup you can put place holder with all css and js references you need like this:

<asp:PlaceHolder ID="phStyles" runat="server">
  <link type="text/css" rel="stylesheet" href="/App_Themes/Default/style/MyStyles.css" />
</asp:PlaceHolder>

Than in the user control's code behind you can add the place holder to the page's controls colection:

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Header.Controls.Add(phStyles);
    }

The only condition is that on the Page the head tag must have runat="server" like this:

<head runat="server">
</head>
Branislav Abadjimarinov
Thanks it worked!!