最近一直在学习.netWeb控件开发,以前在C/s中用习惯了GroupBox控件,在如今的asp.net中也想使用这样的现成控件,只不过一直以来没有找到;于是前几天就在想为何自己不写一个这样的控件来使用,今天完成了这个控件,贴出来和大家一起分享和学习。
     详细代码如下:
   

 Code
Code
  1 using System;
using System;
  2 using System.Collections.Generic;
using System.Collections.Generic;
  3 using System.ComponentModel;
using System.ComponentModel;
  4 using System.Text;
using System.Text;
  5 using System.Web;
using System.Web;
  6 using System.Web.UI;
using System.Web.UI;
  7 using System.Web.UI.WebControls;
using System.Web.UI.WebControls;
  8
  9 namespace MyWebControls
namespace MyWebControls
 10

 {
{
 11 [DefaultProperty("Text")]
    [DefaultProperty("Text")]
 12 [ToolboxData("<{0}:WebGroupBox runat=server></{0}:WebGroupBox>")]
    [ToolboxData("<{0}:WebGroupBox runat=server></{0}:WebGroupBox>")]
 13 public class WebGroupBox : Panel
    public class WebGroupBox : Panel
 14
 
     {
{
 15
 16
 WebGruopBox 属性WebGruopBox 属性#region WebGruopBox 属性
        WebGruopBox 属性WebGruopBox 属性#region WebGruopBox 属性
 17
 /**//**//**//// <summary>
        /**//**//**//// <summary>
 18 /// WebGroupBox 宽度
        /// WebGroupBox 宽度
 19 /// </summary>
        /// </summary>
 20 [Bindable(true)]
        [Bindable(true)]
 21 [Category("WebGroupBox")]
        [Category("WebGroupBox")]
 22 [DefaultValue("")]
        [DefaultValue("")]
 23 [Localizable(true)]
        [Localizable(true)]
 24 public override Unit Width
        public override Unit Width
 25
 
         {
{
 26 get
            get 
 27
 
             {
{ 
 28 return base.Width;
                return base.Width;
 29 }
            }
 30 set
            set 
 31
 
             {
{ 
 32 base.Width = value;
                base.Width = value;
 33 }
            }
 34 }
        }
 35 
        
 36 [Bindable(false), Browsable(false), Category("WebGroupBox"), DefaultValue("")]
        [Bindable(false), Browsable(false), Category("WebGroupBox"), DefaultValue("")]
 37 public override string GroupingText
        public override string GroupingText
 38
 
         {
{
 39 get
            get
 40
 
             {
{
 41 return base.GroupingText;
                return base.GroupingText;
 42 }
            }
 43 set
            set
 44
 
             {
{
 45 base.GroupingText = value;
                base.GroupingText = value;
 46 }
            }
 47 }
        }
 48 
        
 49
 /**//**//**//// <summary>
        /**//**//**//// <summary>
 50 /// WebGroupBox 高度
        /// WebGroupBox 高度
 51 /// </summary>
        /// </summary>
 52 [Bindable(true)]
        [Bindable(true)]
 53 [Category("WebGroupBox")]
        [Category("WebGroupBox")]
 54 [DefaultValue("")]
        [DefaultValue("")]
 55 [Description("GroupBox 的宽")]
        [Description("GroupBox 的宽")]
 56 public override Unit Height
        public override Unit Height
 57
 
         {
{
 58 get
            get 
 59
 
             {
{ 
 60 return base.Height;
                return base.Height;
 61 }
            }
 62 set
            set 
 63
 
             {
{ 
 64 base.Height = value;
                base.Height = value;
 65 }
            }
 66 }
        }
 67
 68 private Align _GroupBoxAlign;
        private Align _GroupBoxAlign;
 69 [Bindable(true)]
        [Bindable(true)]
 70 [Category("WebGroupBox")]
        [Category("WebGroupBox")]
 71 [DefaultValue("")]
        [DefaultValue("")]
 72 [Localizable(true)]
        [Localizable(true)]
 73 public Align Align
        public Align Align
 74
 
         {
{
 75 get
            get
 76
 
             {
{
 77 return _GroupBoxAlign;
                return _GroupBoxAlign;
 78 }
            }
 79 set
            set
 80
 
             {
{
 81 _GroupBoxAlign = value;
                _GroupBoxAlign = value;
 82 }
            }
 83 }
        }
 84 private WebGroupStyleColor _StyleColor;
        private WebGroupStyleColor _StyleColor;
 85 [Bindable(true), Category("WebGroupBox"), DefaultValue("")]
        [Bindable(true), Category("WebGroupBox"), DefaultValue("")]
 86 public WebGroupStyleColor WebGroupBoxBorderColor
        public WebGroupStyleColor WebGroupBoxBorderColor
 87
 
         {
{
 88 get
            get
 89
 
             {
{
 90 return _StyleColor;
                return _StyleColor;
 91 }
            }
 92 set
            set
 93
 
             {
{
 94 _StyleColor = value;
                _StyleColor = value;
 95 }
            }
 96 }
        }
 97
 98
 /**//**//**//// <summary>
        /**//**//**//// <summary>
 99 /// WebGroupBox FieldsetStyle样式
        /// WebGroupBox FieldsetStyle样式
100 /// </summary>
        /// </summary>
101 private string _fieldsetstyle = "border: green 1px solid; padding: 4px 30px 10px 30px;  margin-bottom: 8px;  text-align: left";
        private string _fieldsetstyle = "border: green 1px solid; padding: 4px 30px 10px 30px;  margin-bottom: 8px;  text-align: left";
102 [Category("外观")]
        [Category("外观")]
103 [Bindable(true)]
        [Bindable(true)]
104 [Description("Fieldset 样式")]
        [Description("Fieldset 样式")]
105 public string FieldsetStyle
        public string FieldsetStyle
106
 
         {
{
107
 get
            get  { return _fieldsetstyle; }
{ return _fieldsetstyle; }
108
 set
            set  { _fieldsetstyle = value; }
{ _fieldsetstyle = value; }
109 }
        }
110
111
 /**//**//**//// <summary>
        /**//**//**//// <summary>
112 /// WebGroupBox LegendStyle样式
        /// WebGroupBox LegendStyle样式
113 /// </summary>
        /// </summary>
114 private string _legendstyle = "font-weight: bold; line-height: 45px";
        private string _legendstyle = "font-weight: bold; line-height: 45px";
115 [Category("外观")]
        [Category("外观")]
116 [Bindable(true)]
        [Bindable(true)]
117 [Description("Legend 样式")]
        [Description("Legend 样式")]
118 public string LegendStyle
        public string LegendStyle
119
 
         {
{
120
 get
            get  { return _legendstyle; }
{ return _legendstyle; }
121
 set
            set  { _legendstyle = value; }
{ _legendstyle = value; }
122 }
        }
123
124 private string _WebGroupBoxTitle;
        private string _WebGroupBoxTitle;
125 [Bindable(true), Browsable(true), Category("WebGroupBox"), DefaultValue("")]
        [Bindable(true), Browsable(true), Category("WebGroupBox"), DefaultValue("")]
126 public string WebGroupBoxTitle
        public string WebGroupBoxTitle
127
 
         {
{
128
 set
            set  { _WebGroupBoxTitle = value; }
{ _WebGroupBoxTitle = value; }
129 get
            get
130
 
             {
{
131 try
                try
132
 
                 {
{
133 if (_WebGroupBoxTitle.Trim() == "")
                    if (_WebGroupBoxTitle.Trim() == "")
134
 
                     {
{
135 _WebGroupBoxTitle = this.UniqueID.ToString();
                        _WebGroupBoxTitle = this.UniqueID.ToString();
136 }
                    }
137 }
                }
138 catch
                catch
139
 
                 {
{
140 _WebGroupBoxTitle = this.UniqueID.ToString();
                    _WebGroupBoxTitle = this.UniqueID.ToString();
141 }
                }
142 
                
143 return _WebGroupBoxTitle;
                return _WebGroupBoxTitle;
144 }
            }
145 }
        }
146 #endregion
        #endregion
147
148 protected override void RenderContents(HtmlTextWriter output)
        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";
            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")
            if (this.WebGroupBoxBorderColor.ToString() != "nocolor")
152
 
             {
{
153 output.AddAttribute(HtmlTextWriterAttribute.Style, FieldStyleStr);
                output.AddAttribute(HtmlTextWriterAttribute.Style, FieldStyleStr);
154 }
            }
155 else
            else
156
 
             {
{
157 output.AddAttribute(HtmlTextWriterAttribute.Style, _fieldsetstyle);
                output.AddAttribute(HtmlTextWriterAttribute.Style, _fieldsetstyle);
158 }
            }
159 if (!Width.IsEmpty)
            if (!Width.IsEmpty)
160
 
             {
{
161 output.AddStyleAttribute(HtmlTextWriterStyle.Width, Width.ToString());
                output.AddStyleAttribute(HtmlTextWriterStyle.Width, Width.ToString());
162 }
            }
163 if (!Height.IsEmpty)
            if (!Height.IsEmpty)
164
 
             {
{
165 output.AddStyleAttribute(HtmlTextWriterStyle.Height, Height.ToString());
                output.AddStyleAttribute(HtmlTextWriterStyle.Height, Height.ToString());
166 }
            }
167 output.RenderBeginTag(HtmlTextWriterTag.Fieldset);
            output.RenderBeginTag(HtmlTextWriterTag.Fieldset);
168 output.AddAttribute(HtmlTextWriterAttribute.Style, _legendstyle);
            output.AddAttribute(HtmlTextWriterAttribute.Style, _legendstyle);
169 output.RenderBeginTag(HtmlTextWriterTag.Legend);
            output.RenderBeginTag(HtmlTextWriterTag.Legend);
170 output.Write(WebGroupBoxTitle);
            output.Write(WebGroupBoxTitle);
171 output.RenderEndTag();
            output.RenderEndTag();
172 base.RenderContents(output);
            base.RenderContents(output);
173 output.RenderEndTag();
            output.RenderEndTag();
174 
            
175 }
        }
176 }
    }
177 }
}
178
   控件样式枚举类:
     
 using System;
using System;
 using System.Collections.Generic;
using System.Collections.Generic;
 using System.Text;
using System.Text;

 namespace MyWebControls
namespace MyWebControls


 {
{
 public enum WebGroupStyleColor
    public enum WebGroupStyleColor

 
     {
{
 nocolor,
        nocolor,
 green,
        green,
 red,
        red,
 Blue,
        Blue,
 Black,
        Black,
 Yellow,
        Yellow,
 SlateGray,
        SlateGray,
 RosyBrown,
        RosyBrown,
 RoyalBlue,
        RoyalBlue,
 SaddleBrown,
        SaddleBrown,
 Salmon,
        Salmon,
 SeaGreen,
        SeaGreen,
 Sienna,
        Sienna,
 Silver,
        Silver,
 SlateBlue,
        SlateBlue,
 SpringGreen,
        SpringGreen,
 Transparent,
        Transparent,
 YellowGreen
        YellowGreen
 }
    }
 }
}

 1 using System;
using System;
 2 using System.Collections.Generic;
using System.Collections.Generic;
 3 using System.Text;
using System.Text;
 4
 5 namespace MyWebControls
namespace MyWebControls
 6

 {
{
 7
 /**//// <summary>
    /**//// <summary>
 8 /// 定义枚举类型控件样式
    /// 定义枚举类型控件样式
 9 /// </summary>
    /// </summary>
10 public enum Align
    public enum Align
11
 
     {
{
12
 /**//// <summary>
        /**//// <summary>
13 /// center of Align
        /// center of Align
14 /// </summary>
        /// </summary>
15 center,
        center,
16
17
 /**//// <summary>
        /**//// <summary>
18 /// left of Align
        /// left of Align
19 /// </summary>
        /// </summary>
20 left,
        left,
21
 /**//// <summary>
        /**//// <summary>
22 /// right of Align
        /// right of Align
23 /// </summary>
        /// </summary>
24 right
        right
25 }
    }
26 }
}
27
 
  我已经做好了被拍砖的心里准备。