Wednesday, June 17, 2009

Hello Friends,
In this article I will share with you small useful code snippet.

Consider a scenario where you want to change the theme of the site which is total css based.So if you want to change the css dynamically you can use following code snippet.

1. Create Object of HtmlLink class.

System.Web.UI.HtmlControls.HtmlLink mycss = new System.Web.UI.HtmlControls.HtmlLink();

2. Change css as per drop down list selection.

string str=ddlTheme.SelectedValue.ToString();

//Drop down list contains theme name and css location.

3.Specify the path of the CSS.

mycss.Href = Server.MapPath(str);//Map the path for css.

4.Add attribute to css.

mycss.Attributes.Add("rel", "stylesheet");

mycss.Attributes.Add("type", "text/css");

5.Add CSS in page header as follow.

Page.Header.Controls.Add(mycss);



No comments: