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 文件
基础教程推荐
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 将数据集转换为列表 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 获取C#保存对话框的文件路径 2022-01-01
- 如果条件可以为空 2022-01-01
