如何为CheckBoxList和RadioButtonList添加滚动条
如何给CheckBoxList和RadioButtonList添加滚动条?
继承基类CheckBoxList和RadioButtonList,添加滚动属性,重写Render方法即可。
属性列表:
#region滚动控制
privatebool_ShowScrollBar=false;
///<summary>
///显示滚动条
///</summary>
[
System.ComponentModel.Description("是否显示显示滚动条")
,System.ComponentModel.DefaultValue(false)
,System.ComponentModel.Category("滚动条设置")
,System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
]
publicboolShowScrollBar
{
get{return_ShowScrollBar;}
set{_ShowScrollBar=value;}
}
privateOverflow_OverflowY=Overflow.auto;
///<summary>
///竖直滚动条
///</summary>
[
System.ComponentModel.Description("竖直滚动条")
,System.ComponentModel.DefaultValue(Overflow.auto)
,System.ComponentModel.Category("滚动条设置")
,System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
]
publicOverflowOverflowY
{
get{return_OverflowY;}
set{_OverflowY=value;}
}
privateOverflow_OverflowX=Overflow.auto;
///<summary>
///水平滚动条
///</summary>
[
System.ComponentModel.Description("水平滚动条")
,System.ComponentModel.DefaultValue(Overflow.auto)
,System.ComponentModel.Category("滚动条设置")
,System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
]
publicOverflowOverflowX
{
get{return_OverflowX;}
set{_OverflowX=value;}
}
privateUnit_ScrollHeight=Unit.Parse("0px");
///<summary>
///滚动高度
///</summary>
[
System.ComponentModel.Description("滚动高度")
,System.ComponentModel.Category("滚动条设置")
,DefaultValue("0px")
,System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
]
publicUnitScrollHeight
{
get{return_ScrollHeight;}
set{_ScrollHeight=value;}
}
privateUnit_ScrollWidth=Unit.Parse("0px");
///<summary>
///滚动宽度
///</summary>
[
System.ComponentModel.Description("滚动宽度")
,System.ComponentModel.Category("滚动条设置")
,DefaultValue("0px")
,System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
]
publicUnitScrollWidth
{
get{return_ScrollWidth;}
set{_ScrollWidth=value;}
}
privatestring_ScrollCssClass="";
///<summary>
///滚动样式设置
///</summary>
[
System.ComponentModel.Description("滚动样式设置")
,System.ComponentModel.Category("滚动条设置")
,System.ComponentModel.DefaultValue("")
,System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
]
publicstringScrollCssClass
{
get{return_ScrollCssClass;}
set{_ScrollCssClass=value;}
}
#region书写标签
voidWriteBeginSpan(HtmlTextWriterwriter)
{
if(this._ShowScrollBar)
{
StringBuilderstrSpan=newStringBuilder();
strSpan.Append("<span");
strSpan.Append(string.Format("style='overflow-y:{0};overflow-x:{1};",
System.Enum.GetName(typeof(Overflow),this._OverflowY),
System.Enum.GetName(typeof(Overflow),this._OverflowX)));
if(this._ScrollHeight.ToString()!="0px")
{
strSpan.Append(string.Format("height:{0};",this._ScrollHeight));
}
if(this._ScrollWidth.ToString()!="0px")
{
strSpan.Append(string.Format("width:{0};",this._ScrollWidth));
}
strSpan.Append("';");
if(!string.IsNullOrEmpty(_ScrollCssClass))
{
strSpan.Append(string.Format("class='{0}'",_ScrollCssClass));
}
strSpan.Append(">");
writer.Write(strSpan.ToString());
}
}
voidWriteEndSpan(HtmlTextWriterwriter)
{
if(this._ShowScrollBar)
{
writer.Write("</span>");
}
}
#endregion
#endregion
重写Render方法:
protectedoverridevoidRender(HtmlTextWriterwriter)
{
this.WriteBeginSpan(writer);
base.Render(writer);
this.WriteEndSpan(writer);
}
就这样就可以了。
还要定义一个枚举:
publicenumOverflow
{
auto=0,
hidden=1,
scroll=2,
visible=3,
inherit=4
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。