方法一:
<%@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">
</from>
</body>
</html>
方法二:
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>
|