RadioButtonList也是数据绑定控件,但是表现的形式远不及repeater,datagrid,datalist这些数据控件多。原因很简单他是radiobutton的列表么。;)但是在实际的工作中往往也需要来点花哨的如下图
今天超子简单写了一个实现上图效果的自定义radiobuttonlist控件,把代码贴出来,大家讨论讨论。
为了方便贴在网上,超子把用到的两个类写在一个文件里了。另外,横向布局的代码我没写,只写了纵向的。
using
System;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Globalization;
namespace
Foto
{
//->**************************************
//-> CustomRadioButtonList Simple Sample *
//-> *
//-> by Chaozi *
//-> 04/11/16 *
//->**************************************
public class MyRadioButtonList:RadioButtonList
{
— 成员变量 —#region — 成员变量 —
private string alternatingItemCssClass;
private string itemCssClass;
#endregion
— 构造 —#region — 构造 —
public MyRadioButtonList()
{
}
#endregion
— 属性 —#region — 属性 —
public string AlternatingItemCssClass
{
get{ return alternatingItemCssClass; }
set{ alternatingItemCssClass = value;}
}
public string ItemCssClass
{
get{ return itemCssClass; }
set{ itemCssClass = value;}
}
#endregion
— 重写呈现方法 —#region — 重写呈现方法 —
protected override void Render(HtmlTextWriter writer)
{
MyRepeatInfo info = new MyRepeatInfo();
Style cStyle = (ControlStyleCreated ? ControlStyle : null);
info.RepeatColumns = RepeatColumns;
info.RenderRepeater(writer,this,cStyle,this,itemCssClass,alternatingItemCssClass);
}
#endregion
}
public class MyRepeatInfo
{
— 成员变量 —#region — 成员变量 —
int repeatColumns = 0;
RepeatDirection direction = RepeatDirection.Vertical;
#endregion
— 构造 —#region — 构造 —
public MyRepeatInfo()
{
}
#endregion
— 属性 —#region — 属性 —
public int RepeatColumns
{
get{ return repeatColumns; }
set{ repeatColumns = value;}
}
public RepeatDirection Direction
{
get{ return direction; }
set{ direction = value;}
}
#endregion
— 方法 —#region — 方法 —
public void RenderRepeater(HtmlTextWriter writer,IRepeatInfoUser user,Style controlStyle,WebControl baseControl)
{
RenderRepeater(writer,user,controlStyle,baseControl,null,null);
}
public void RenderRepeater(HtmlTextWriter writer,IRepeatInfoUser user,Style controlStyle,WebControl baseControl,string alternatingItemCssClass)
{
RenderRepeater(writer,user,controlStyle,baseControl,null,alternatingItemCssClass);
}
public void RenderRepeater(HtmlTextWriter writer,IRepeatInfoUser user,Style controlStyle,WebControl baseControl,string itemCssClass,string alternatingItemCssClass)
{
switch (direction)
{
case RepeatDirection.Horizontal:
RenderHorizontal(writer,user,controlStyle,baseControl,itemCssClass,alternatingItemCssClass);
break;
case RepeatDirection.Vertical:
RenderVertical(writer,user,controlStyle,baseControl,itemCssClass,alternatingItemCssClass);
break;
}
}
#endregion
— 辅助方法 —#region — 辅助方法 —
void RenderHorizontal(HtmlTextWriter writer,IRepeatInfoUser user,Style controlStyle,WebControl baseControl,string itemCssClass,string alternatingItemCssClass)
{
//水平
}
//->
void RenderVertical(HtmlTextWriter writer,IRepeatInfoUser user,Style controlStyle,WebControl baseControl,string itemCssClass,string alternatingItemCssClass)
{
//垂直–
int total = user.RepeatedItemCount;
int colsCount = repeatColumns;
if(colsCount == 0)
colsCount = 1;
WebControl ctrl = new Table();
ctrl.ID = baseControl.ClientID;
ctrl.CopyBaseAttributes(baseControl);
ctrl.ApplyStyle(controlStyle);
string itemCss;
ctrl.RenderBeginTag(writer);
for ( int index = 0; index < total ; index ++)
{
if(index % colsCount == 0)
{
if((index / colsCount) % 2 == 0)
itemCss = itemCssClass;
else
itemCss = alternatingItemCssClass;
writer.AddAttribute(HtmlTextWriterAttribute.Class,itemCss);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
}
writer.RenderBeginTag(HtmlTextWriterTag.Td);
RepeatInfo info = new RepeatInfo();
info.RepeatColumns = repeatColumns;
user.RenderItem(ListItemType.Item,index,info,writer);
writer.RenderEndTag();
if(((index+1) % colsCount == 0) || (index+1 == total))
writer.RenderEndTag();
}
ctrl.RenderEndTag(writer);
}
#endregion
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/36885.html