以编程方式将样式表和 javascript 引用添加到 ASP.NET 母版页

发布于 2024-10-22 02:54:31 字数 166 浏览 6 评论 0原文

我正在使用 Lightbox,但我只希望对样式表和 javascript 文件的引用位于网站某一页面(使用 Lightbox 的页面)的母版页标题中。如何以编程方式在页面加载中添加对样式表和 javascript 文件的引用?

样式表是“css”文件夹,三个 javascript 文件是“js”文件夹

I'm using Lightbox but i only want the references to the stylesheet and javascript files to be in the masterpage header on one page on the site (the page that uses lightbox). how do I programmatically add references to the stylesheet and javascript files in the page load?

the stylesheet is the 'css' folder and the three javascript files are a 'js' folder

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终止放荡 2024-10-29 02:54:31

尝试...

Page.ClientScript.RegisterClientScriptInclude("JScripts", ResolveUrl("~/js/JScripts.js"));

try...

Page.ClientScript.RegisterClientScriptInclude("JScripts", ResolveUrl("~/js/JScripts.js"));
眼波传意 2024-10-29 02:54:31

将两个占位符(“JsPlaceholder”和“CSSPlaceholder”)添加到母版页上的标题并调用这些方法:

public void AddJavascriptFile(string path)
{
    PlaceHolder p = (PlaceHolder)Page.Header.FindControl("JsPlaceholder");
    p.Controls.Add(new LiteralControl(string.Concat("<script type='text/javascript' src='", path, "'></script>\n")));
}

public void AddCssFile(string urlPath)
{
    HtmlLink cssLink = new HtmlLink();
    cssLink.Href = path;
    cssLink.Attributes.Add("rel", "stylesheet");
    cssLink.Attributes.Add("type", "text/css");
    PlaceHolder p = (PlaceHolder)Page.Header.FindControl("CssPlaceholder");
    p.Controls.Add(cssLink);
}

Add two placeholders ("JsPlaceholder" and "CSSPlaceholder") to your header on master page and call those methods:

public void AddJavascriptFile(string path)
{
    PlaceHolder p = (PlaceHolder)Page.Header.FindControl("JsPlaceholder");
    p.Controls.Add(new LiteralControl(string.Concat("<script type='text/javascript' src='", path, "'></script>\n")));
}

public void AddCssFile(string urlPath)
{
    HtmlLink cssLink = new HtmlLink();
    cssLink.Href = path;
    cssLink.Attributes.Add("rel", "stylesheet");
    cssLink.Attributes.Add("type", "text/css");
    PlaceHolder p = (PlaceHolder)Page.Header.FindControl("CssPlaceholder");
    p.Controls.Add(cssLink);
}
饮湿 2024-10-29 02:54:31

尝试(在 C# 中,但你应该明白):

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    HtmlLink cssLink = new HtmlLink();

    //Create and configure the CSS link.
    cssLink.Attributes.Add("rel", "Stylesheet");
    cssLink.Attributes.Add("type", "text/css");
    cssLink.Href = "~/Path/To/File.css";

    //Add the CSS link to the page header.
    this.Header.Controls.Add(cssLink);

    //Add a script include to the page's ClientScript.
    this.ClientScript.RegisterClientScriptInclude("NameOfScript", this.ResolveUrl("~/Path/To/File.js"));
}

Try (in C# but you should get the idea):

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    HtmlLink cssLink = new HtmlLink();

    //Create and configure the CSS link.
    cssLink.Attributes.Add("rel", "Stylesheet");
    cssLink.Attributes.Add("type", "text/css");
    cssLink.Href = "~/Path/To/File.css";

    //Add the CSS link to the page header.
    this.Header.Controls.Add(cssLink);

    //Add a script include to the page's ClientScript.
    this.ClientScript.RegisterClientScriptInclude("NameOfScript", this.ResolveUrl("~/Path/To/File.js"));
}
那请放手 2024-10-29 02:54:31

您可以这样做,

将其添加到您的标头中:

  <asp:placeholder runat="server" id="lightbox" visible="false">

        <link rel="stylesheet" href="/css/style.css" />
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"</script>
        </asp:placeholder>

并从您的代码隐藏集中

lightbox.visible=true;

您还应该注意,通常您希望在页面上保留尽可能多的 html,而不是将其放在代码隐藏中,这样设计人员就可以轻松地制作变化

you can do it like this

added this to your header:

  <asp:placeholder runat="server" id="lightbox" visible="false">

        <link rel="stylesheet" href="/css/style.css" />
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"</script>
        </asp:placeholder>

and from your codebehind set

lightbox.visible=true;

You should also note that normally you want to keep as much html on the page instead of having it in your codebehind so it will be easy for the designer to make changes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文