Implement a multi-interface (similar to this blog) by enabling pages to dynamically load different CSS
Method One:
<%@page language="C#"%>
<%@import namespace="System.Data"%>
<script language="c#" runat="server">
public void page_load(Object obj,EventArgs e)
{
//创建服务器端控件.
//指定的标记"LINK"初始化此类的新实例.
HtmlGenericControl objLink=new HtmlGenericControl("LINK");
objLink.ID=ID;
objLink.Attributes["rel"]="stylesheet";
objLink.Attributes["type"]="text/css";
objLink.Attributes["href"]="portal.css";
//此控件不产生任何可见输出,仅作为其他控件的容器,可在其中添加,插入或移除控件.
MyCSS.Controls.Add(objLink);
}
</script>
<html>
<head>
<title>c#</title>
<asp:placeholder id="MyCSS" runat="server"></asp:placeholder>
</head>
<body bgColor="#ffcc66" style="FONT:9pt">
<form runat="server">
</form>
</body>
</html>
==========================================
Change the interface by dynamically setting the style of all the same controls on the page:
Method Two:
You can easily set and modify the style of a control by changing the CssClass property of the Web control.
However, in the actual development process, set the control of the CssClass property, very cumbersome, so this idea is not widely used.
But the following code snippet shows a way to change the style of all the same controls on a page at once, with simple skin features.
The code is as follows:
public void page_load(Object obj,EventArgs e)
{
if(!Page.IsPostBack){
//为页面的所有控件设置样式.
SetCSS(Page.Controls);
}
}
private void SetCSS(System.Web.UI.ControlCollection vControls)
{
for(int i=0;i<vControls.Count;i++)
{
System.Web.UI.Control vControl=vControls[i];
//得到控件的类型
//可增加控件类型及相应处理方法
string PType=vControl.GetType().Name;
switch (PType)
{
case "TextBox":
TextBox_CSS ((TextBox) vControl);
break;
case "Button":
//Button_CSS ((Button) vControl);
break;
case "DataGrid":
//DataGrid_CSS ((DataGrid) vControl);
break;
}
if(vControl.Controls.Count>0)
SetCSS(vControl.Controls);
}
}
private void TextBox_CSS(TextBox tb){
tb.CssClass="TextBox_show";
}
<form runat="server">
<asp:textbox id="Search1" runat="server"/>
<asp:textbox id="Search2" CssClass="INPUT" runat="server"/>
</form>
After running, view the page source code. You can find that the style of the text box has been uniformly modified to "textbox_show".