通过使页面动态加载不同CSS实现多界面
方法一:
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@page language="C#"%>
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@import namespace="System.Data"%>
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script language="c#" runat="server">
public void page_load(Object obj,EventArgs e)
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//创建服务器端控件.
//指定的标记"LINK"初始化此类的新实例.
HtmlGenericControl objLink=new HtmlGenericControl("LINK");
objLink.ID=ID;
objLink.Attributes["rel"]="stylesheet";
objLink.Attributes["type"]="text/css";
objLink.Attributes["href"]="portal.css";
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//此控件不产生任何可见输出,仅作为其他控件的容器,可在其中添加,插入或移除控件.
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">
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
</form>
</body>
</html>
通过动态设置页面所有同类型控件的样式来该变界面:
方法二:
可以通过改变WEB控件的CssClass属性,可方便地设置和修改控件的样式。
但在实际开发过程中,一个个地设置控件的CssClass属性,非常繁琐,所以此思路应用不广.
但下面的代码段演示了一次性改变页面所有同类型控件的样式的方法,可以实现简单的SKIN等功能。
代码如下:
public void page_load(Object obj,EventArgs e)
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(!Page.IsPostBack)
{
//为页面的所有控件设置样式.
SetCSS(Page.Controls);
}
}
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
private void SetCSS(System.Web.UI.ControlCollection vControls)
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
for(int i=0;i<vControls.Count;i++)
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
System.Web.UI.Control vControl=vControls[i];
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//得到控件的类型
//可增加控件类型及相应处理方法
string PType=vControl.GetType().Name;
switch (PType)
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
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);
}
}
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
private void TextBox_CSS(TextBox tb)
{
tb.CssClass="TextBox_show";
}
![](https://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
<form runat="server">
<asp:textbox id="Search1" runat="server"/>
<asp:textbox id="Search2" CssClass="INPUT" runat="server"/>
</form>