Dynamically adding a CSS file from an ASP.NET Server Control(从 ASP.NET 服务器控件动态添加 CSS 文件)
问题描述
我有一个自定义控件,我想动态插入一个指向样式表的链接.
I have a custom control and I want to dynamically insert a link to a stylesheet.
可能不是今年最好的解决方案,但它需要完成.知道怎么做吗?
Might not be the best solution of the year but it needs to be done. Any idea how to do it?
每次我尝试时,Page.Header 都是空的.
Everytime I try, Page.Header is null.
推荐答案
以下是您通常以编程方式添加 CSS 的方式:
Here's how you would normally add a CSS programatically:
protected void Page_Init(object sender, EventArgs e)
{
var link = new HtmlLink();
link.Href = "~/styles/main.css";
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("type", "text/css");
Page.Header.Controls.Add(link);
}
您可能需要将 runat="server" 放在 head 标签中:
You might need to put a runat="server" in the head tag:
<head runat="server">
<title>Add CSS example</title>
</head>
这篇关于从 ASP.NET 服务器控件动态添加 CSS 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 ASP.NET 服务器控件动态添加 CSS 文件
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
