我正在使用Lightbox,但我只希望对样式表和javascript文件的引用位于网站上一个页面(使用灯箱的页面)的母版页标题中。如何以编程方式在页面加载中添加对样式表和javascript文件的引用?
样式表是'css'文件夹,三个javascript文件是'js'文件夹
答案 0 :(得分:2)
尝试...
Page.ClientScript.RegisterClientScriptInclude("JScripts", ResolveUrl("~/js/JScripts.js"));
答案 1 :(得分:1)
将两个占位符(“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);
}
答案 2 :(得分:1)
你可以这样做
将此添加到标题中:
<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而不是在代码隐藏中使用它,这样设计人员就可以轻松进行更改
答案 3 :(得分:1)
尝试(在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"));
}