给CheckBoxList和RadioButtonList添加滚动条

如何给CheckBoxList和RadioButtonList添加滚动条??
继承基类CheckBoxList和RadioButtonList,添加滚动属性,重写Render方法即可。
属性列表:

 

  1        #region 滚动控制
  2         private bool _ShowScrollBar = false;
  3         /// <summary>
  4         /// 显示滚动条
  5         /// </summary>
  6         [
  7        System.ComponentModel.Description("是否显示显示滚动条")
  8        , System.ComponentModel.DefaultValue(false)
  9        , System.ComponentModel.Category("滚动条设置")
 10        , System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
 11        ]
 12         public bool ShowScrollBar
 13         {
 14             get { return _ShowScrollBar; }
 15             set { _ShowScrollBar = value; }
 16         }
 17         private Overflow _OverflowY = Overflow.auto;
 18         /// <summary>
 19         /// 竖直滚动条
 20         /// </summary>
 21         [
 22        System.ComponentModel.Description("竖直滚动条")
 23        , System.ComponentModel.DefaultValue(Overflow.auto)
 24        , System.ComponentModel.Category("滚动条设置")
 25        , System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
 26        ]
 27         public Overflow OverflowY
 28         {
 29             get { return _OverflowY; }
 30             set { _OverflowY = value; }
 31         }
 32         private Overflow _OverflowX = Overflow.auto;
 33         /// <summary>
 34         /// 水平滚动条
 35         /// </summary>
 36         [
 37        System.ComponentModel.Description("水平滚动条")
 38        , System.ComponentModel.DefaultValue(Overflow.auto)
 39        , System.ComponentModel.Category("滚动条设置")
 40        , System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
 41        ]
 42         public Overflow OverflowX
 43         {
 44             get { return _OverflowX; }
 45             set { _OverflowX = value; }
 46         }
 47         private Unit _ScrollHeight = Unit.Parse("0px");
 48         /// <summary>
 49         /// 滚动高度
 50         /// </summary>
 51         [
 52        System.ComponentModel.Description("滚动高度")
 53        , System.ComponentModel.Category("滚动条设置")
 54          , DefaultValue("0px")
 55        , System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
 56        ]
 57         public Unit ScrollHeight
 58         {
 59             get { return _ScrollHeight; }
 60             set { _ScrollHeight = value; }
 61         }
 62         private Unit _ScrollWidth = Unit.Parse("0px");
 63         /// <summary>
 64         /// 滚动宽度
 65         /// </summary>
 66         [
 67        System.ComponentModel.Description("滚动宽度")
 68        , System.ComponentModel.Category("滚动条设置")
 69         , DefaultValue("0px")
 70        , System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
 71        ]
 72         public Unit ScrollWidth
 73         {
 74             get { return _ScrollWidth; }
 75             set { _ScrollWidth = value; }
 76         }
 77         private string _ScrollCssClass = "";
 78         /// <summary>
 79         /// 滚动样式设置
 80         /// </summary>
 81         [
 82        System.ComponentModel.Description("滚动样式设置")
 83        , System.ComponentModel.Category("滚动条设置")
 84        , System.ComponentModel.DefaultValue("")
 85        , System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
 86        ]
 87         public string ScrollCssClass
 88         {
 89             get { return _ScrollCssClass; }
 90             set { _ScrollCssClass = value; }
 91         }
 92 
 93         #region 书写标签
 94         void WriteBeginSpan(HtmlTextWriter writer)
 95         {
 96             if (this._ShowScrollBar)
 97             {
 98                 StringBuilder strSpan = new StringBuilder();
 99                 strSpan.Append("<span ");
100                 strSpan.Append(string.Format("style='overflow-y:{0};overflow-x:{1};",
101                     System.Enum.GetName(typeof(Overflow), this._OverflowY),
102                     System.Enum.GetName(typeof(Overflow), this._OverflowX)));
103                 if (this._ScrollHeight.ToString() != "0px")
104                 {
105                     strSpan.Append(string.Format("height:{0};"this._ScrollHeight));
106                 }
107                 if (this._ScrollWidth.ToString() != "0px")
108                 {
109                     strSpan.Append(string.Format("width:{0};"this._ScrollWidth));
110                 }
111                 strSpan.Append("';");
112                 if (!string.IsNullOrEmpty(_ScrollCssClass))
113                 {
114                     strSpan.Append(string.Format(" class='{0}'", _ScrollCssClass));
115                 }
116                 strSpan.Append(">");
117                 writer.Write(strSpan.ToString());
118             }
119         }
120         void WriteEndSpan(HtmlTextWriter writer)
121         {
122             if (this._ShowScrollBar)
123             {
124                 writer.Write("</span>");
125             }
126         }
127         #endregion
128         #endregion

 

 

重写Render方法:

        protected override void Render(HtmlTextWriter writer)
        {
            
this.WriteBeginSpan(writer);
            
base.Render(writer);
            
this.WriteEndSpan(writer);
        }

 

 

就这样就可以了。
还要定义一个枚举:

1     public enum Overflow
2     {
3         auto = 0,
4         hidden = 1,
5         scroll = 2,
6         visible = 3,
7         inherit = 4
8     }

 

 

posted @ 2008-04-18 16:40  迪克猪  阅读(2790)  评论(6编辑  收藏  举报