开发者

How to set style sheet from server side in asp.net

开发者 https://www.devze.com 2023-02-14 09:00 出处:网络
I am using the style sheets for the gridview from this link http://www.cyberslingers.com/Sandbox/GridView.aspx

I am using the style sheets for the gridview from this link

http://www.cyberslingers.com/Sandbox/GridView.aspx

It has various style sheet attached to the same gridview. What 开发者_C百科I want to attach the style sheet from server side to the page when user select the drop down list item


You can generate a stylesheet on the server side and attach it to the page using:

HtmlGenericControl style = new HtmlGenericControl("style");
style.Attributes.Add("type", "text/css");
style.InnerText = "p { color:red; }"; 
Page.Header.Controls.Add(style);

The code above can be inside the event handler of the selectionindexchanged event of the dropdown, you can then generate all the styles you want and attach them to the page.

If you simply want to attach a reference to an external css file then you can do:

HtmlGenericControl link = new HtmlGenericControl("link");
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("type", "text/css");
link.Attributes.Add("href", "/styles.css");
Page.Header.Controls.Add(link);


The simple solution is to just put runat="server" id="myname" in your tag, then you can set the value in code.

HTML

<link id="MyLink" href="~/css/default.css" rel="stylesheet" type="text/css" media="all" runat="server" />

CS

MyLink.Href = "~/css/Different.css";
0

精彩评论

暂无评论...
验证码 换一张
取 消