最近一直在学习.netWeb控件开发,以前在C/s中用习惯了GroupBox控件,在如今的asp.net中也想使用这样的现成控件,只不过一直以来没有找到;于是前几天就在想为何自己不写一个这样的控件来使用,今天完成了这个控件,贴出来和大家一起分享和学习。
详细代码如下:
Code
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Text;
5using System.Web;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8
9namespace MyWebControls
10{
11 [DefaultProperty("Text")]
12 [ToolboxData("<{0}:WebGroupBox runat=server></{0}:WebGroupBox>")]
13 public class WebGroupBox : Panel
14 {
15
16 WebGruopBox 属性WebGruopBox 属性#region WebGruopBox 属性
17 /**//**//**//// <summary>
18 /// WebGroupBox 宽度
19 /// </summary>
20 [Bindable(true)]
21 [Category("WebGroupBox")]
22 [DefaultValue("")]
23 [Localizable(true)]
24 public override Unit Width
25 {
26 get
27 {
28 return base.Width;
29 }
30 set
31 {
32 base.Width = value;
33 }
34 }
35
36 [Bindable(false), Browsable(false), Category("WebGroupBox"), DefaultValue("")]
37 public override string GroupingText
38 {
39 get
40 {
41 return base.GroupingText;
42 }
43 set
44 {
45 base.GroupingText = value;
46 }
47 }
48
49 /**//**//**//// <summary>
50 /// WebGroupBox 高度
51 /// </summary>
52 [Bindable(true)]
53 [Category("WebGroupBox")]
54 [DefaultValue("")]
55 [Description("GroupBox 的宽")]
56 public override Unit Height
57 {
58 get
59 {
60 return base.Height;
61 }
62 set
63 {
64 base.Height = value;
65 }
66 }
67
68 private Align _GroupBoxAlign;
69 [Bindable(true)]
70 [Category("WebGroupBox")]
71 [DefaultValue("")]
72 [Localizable(true)]
73 public Align Align
74 {
75 get
76 {
77 return _GroupBoxAlign;
78 }
79 set
80 {
81 _GroupBoxAlign = value;
82 }
83 }
84 private WebGroupStyleColor _StyleColor;
85 [Bindable(true), Category("WebGroupBox"), DefaultValue("")]
86 public WebGroupStyleColor WebGroupBoxBorderColor
87 {
88 get
89 {
90 return _StyleColor;
91 }
92 set
93 {
94 _StyleColor = value;
95 }
96 }
97
98 /**//**//**//// <summary>
99 /// WebGroupBox FieldsetStyle样式
100 /// </summary>
101 private string _fieldsetstyle = "border: green 1px solid; padding: 4px 30px 10px 30px; margin-bottom: 8px; text-align: left";
102 [Category("外观")]
103 [Bindable(true)]
104 [Description("Fieldset 样式")]
105 public string FieldsetStyle
106 {
107 get { return _fieldsetstyle; }
108 set { _fieldsetstyle = value; }
109 }
110
111 /**//**//**//// <summary>
112 /// WebGroupBox LegendStyle样式
113 /// </summary>
114 private string _legendstyle = "font-weight: bold; line-height: 45px";
115 [Category("外观")]
116 [Bindable(true)]
117 [Description("Legend 样式")]
118 public string LegendStyle
119 {
120 get { return _legendstyle; }
121 set { _legendstyle = value; }
122 }
123
124 private string _WebGroupBoxTitle;
125 [Bindable(true), Browsable(true), Category("WebGroupBox"), DefaultValue("")]
126 public string WebGroupBoxTitle
127 {
128 set { _WebGroupBoxTitle = value; }
129 get
130 {
131 try
132 {
133 if (_WebGroupBoxTitle.Trim() == "")
134 {
135 _WebGroupBoxTitle = this.UniqueID.ToString();
136 }
137 }
138 catch
139 {
140 _WebGroupBoxTitle = this.UniqueID.ToString();
141 }
142
143 return _WebGroupBoxTitle;
144 }
145 }
146 #endregion
147
148 protected override void RenderContents(HtmlTextWriter output)
149 {
150 string FieldStyleStr = "border: " + this.WebGroupBoxBorderColor.ToString() + " 1px solid; padding: 4px 30px 10px 30px; margin-bottom: 8px; text-align: left";
151 if (this.WebGroupBoxBorderColor.ToString() != "nocolor")
152 {
153 output.AddAttribute(HtmlTextWriterAttribute.Style, FieldStyleStr);
154 }
155 else
156 {
157 output.AddAttribute(HtmlTextWriterAttribute.Style, _fieldsetstyle);
158 }
159 if (!Width.IsEmpty)
160 {
161 output.AddStyleAttribute(HtmlTextWriterStyle.Width, Width.ToString());
162 }
163 if (!Height.IsEmpty)
164 {
165 output.AddStyleAttribute(HtmlTextWriterStyle.Height, Height.ToString());
166 }
167 output.RenderBeginTag(HtmlTextWriterTag.Fieldset);
168 output.AddAttribute(HtmlTextWriterAttribute.Style, _legendstyle);
169 output.RenderBeginTag(HtmlTextWriterTag.Legend);
170 output.Write(WebGroupBoxTitle);
171 output.RenderEndTag();
172 base.RenderContents(output);
173 output.RenderEndTag();
174
175 }
176 }
177}
178
控件样式枚举类:
using System;
using System.Collections.Generic;
using System.Text;
namespace MyWebControls
{
public enum WebGroupStyleColor
{
nocolor,
green,
red,
Blue,
Black,
Yellow,
SlateGray,
RosyBrown,
RoyalBlue,
SaddleBrown,
Salmon,
SeaGreen,
Sienna,
Silver,
SlateBlue,
SpringGreen,
Transparent,
YellowGreen
}
}
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace MyWebControls
6{
7 /**//// <summary>
8 /// 定义枚举类型控件样式
9 /// </summary>
10 public enum Align
11 {
12 /**//// <summary>
13 /// center of Align
14 /// </summary>
15 center,
16
17 /**//// <summary>
18 /// left of Align
19 /// </summary>
20 left,
21 /**//// <summary>
22 /// right of Align
23 /// </summary>
24 right
25 }
26}
27
我已经做好了被拍砖的心里准备。