Enter code in masterpage / html head from user control

I am struggling with something that, in my opinion, should be standard practice. I have several user controls that use some jQuery plugins. I really don’t want to link to additional CSS and JS files from my main home page, as this can cause an additional load for the user on the first launch of the site (admittedly, this will be only once), so I just installed they are connected to the top part of the user control. Then I looked at my HTML source code, right! Even worse for controls that repeat several times on a page.

So, I was wondering if there is a way to insert them into the Page Head when they are needed from User Control. In this regard, is there a way to do this for the footer for JS stuff?

+3
source share
5 answers

To dynamically register a script (and make sure duplicates are merged) in ASP.NET you can call:

Page.ClientScript.RegisterClientScriptInclude(
    "mykey", "~/scripts/jquery-1.3.2.js");

And read the full details of this MSDN method .

To add CSS dynamically, you can do something like this:

HtmlLink cssLink = new HtmlLink();
cssLink.Href = "path to CSS";
cssLink.Attributes["some attr1"] = "some value1";
cssLink.Attributes["some attr2"] = "some value2";
Page.Header.Controls.Add(cssLink);

CSS . , . , , , HttpContext.Items. HashSet , , CSS ( , - ).

+8

ClientScript.RegisterClientScriptInclude() JavaScript.

CSS , , Visible="false", .

Items , OnLoad(). , this.Context.Items["mycss"] = true;

, , OnPreRender(), , . , Visible true CSS.

, CSS, . -, , -.

+1

, CSS , CSS. :

<style id="style1" type="text/css" visible="false" runat="server">
    td { font-family: Tahoma; font-size: 8pt; }
</style>

:

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

    HtmlGenericControl style = new HtmlGenericControl("style");
    style.Attributes.Add("type", "text/css");
    style.InnerHtml = style1.InnerHtml;
    Page.Header.Controls.Add(style);
}

, CSS , body.

+1

, Asp.NET.

- MasterPage...

<head>
<asp:ContentPlaceHolder ID="AdditionalPageHeader" />
</head>

aspx ascx, ...

<asp:Content ContentPlaceHolderID="AdditionalPageHeader" />

, :

this.Page.Master.FindControl("AdditionalPageHeader")

... .

0

To add styles or javascript (built-in or not) dynamic, I wrote these three functions:

  Public Function addScript(ByVal path2js As String) As System.Web.UI.Control
    Dim si As New HtmlGenericControl
    si.TagName = "script"
    si.Attributes.Add("type", "text/javascript")
    si.Attributes.Add("src", path2js)
    Return si
  End Function
  Public Function addScript_inline(ByVal js As String) As System.Web.UI.Control
    Dim si As New HtmlGenericControl
    si.TagName = "script"
    si.Attributes.Add("type", "text/javascript")
    si.InnerHtml = js
    Return si
  End Function
  Public Function addStyle(ByVal path2css As String) As System.Web.UI.Control
    Dim css As New HtmlLink
    css.Href = path2css
    css.Attributes.Add("rel", "stylesheet")
    css.Attributes.Add("type", "text/css")
    css.Attributes.Add("media", "all")
    Return css
  End Function

I call them in page_load on my main page, for example:

Me.Page.Header.Controls.Add(modGlobal.addScript("script/json/json2.js"))

or

Me.Page.Header.Controls.Add(modGlobal.addStyle("style/flexigrid/flexigrid.css"))

Hi

0
source

Source: https://habr.com/ru/post/1727982/


All Articles