页面上指定类型的控件的样式添加

重构代码!由于不能用到skin所以在基类里面写了个加载样式的方法。

 1 #region 初始化控件
 2     private void InitializeControls()
 3     {
 4         foreach (Control HtmlCon in this.Controls)
 5         {
 6             if (HtmlCon is HtmlForm)
 7             {
 8                 foreach (Control control in HtmlCon.Controls)
 9                 {
10                     //如果该控件包含有子控件,则调用递归方法初始化子控件
11                     if (control.HasControls())
12                     {
13                         InitializeChildControl(control.Controls);
14                     }
15                     else
16                     {
17                         InitializeControl(control);
18                     }
19                 }
20             }
21         }
22     }
23 
24     /// <summary>
25     /// 有些控件可能包含有子控件,例如Panel,该方法通过递归初始化控件的子控件
26     /// </summary>
27     /// <param name="controls"></param>
28     private void InitializeChildControl(ControlCollection controls)
29     {
30         foreach (Control control in controls)
31         {
32             if (control.HasControls())
33             {
34                 InitializeChildControl(control.Controls);
35             }
36             else
37             {
38                 InitializeControl(control);
39             }
40         }
41     }
42 
43     /// <summary>
44     /// 初始化某一个控件
45     /// </summary>
46     /// <param name="control"></param>
47     private void InitializeControl(Control control)
48     {
49         Type t = control.GetType();
50         switch (t.Name)
51         {
52             case "TextBox":
53                 TextBox textBox = control as TextBox;
54                 if (textBox != null)
55                 {
56                     textBox.CssClass = "bor_gray";
57                     textBox.Attributes.Add("onmouseout", "this.className='bor_gray'");//当文本框获得焦点时,改变文本框背景色
58                     textBox.Attributes.Add("onmouseover", "this.className='bor_gray_a'");//当文本框失去焦点时,回复文本框背景色
59                 }
60                 break;
61             case "HtmlInputText":
62                 HtmlInputText Intext = control as HtmlInputText;
63                 if (Intext != null)
64                 {
65                     Intext.Attributes.Add("class", "bor_gray");
66                     Intext.Attributes.Add("onmouseout", "bor_gray");
67                     Intext.Attributes.Add("onmouseover", "bor_gray_a");
68                 }
69                 break;
70         }
71     }
72     #endregion           

转载于:https://www.cnblogs.com/wxwu/archive/2012/06/22/2558551.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值