自定义RadioButtonList

RadioButtonList也是数据绑定控件,但是表现的形式远不及repeater,datagrid,datalist这些数据控件多。原因很简单他是radiobutton的列表么。;)但是在实际的工作中往往也需要来点花哨的如下图
radiobuttonlist.gif

今天超子简单写了一个实现上图效果的自定义radiobuttonlist控件,把代码贴出来,大家讨论讨论。
为了方便贴在网上,超子把用到的两个类写在一个文件里了。另外,横向布局的代码我没写,只写了纵向的。

None.gif using  System;
None.gif
using  System.Web;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Globalization;
None.gif
None.gif
namespace  Foto
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//->**************************************
InBlock.gif    
//-> CustomRadioButtonList Simple Sample *
InBlock.gif    
//->                                     *
InBlock.gif    
//->                        by Chaozi    *
InBlock.gif    
//->                        04/11/16     *
InBlock.gif    
//->**************************************
InBlock.gif
    public class MyRadioButtonList:RadioButtonList
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-- 成员变量 --#region -- 成员变量 --
InBlock.gif        
private string alternatingItemCssClass;
InBlock.gif        
private string itemCssClass;
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-- 构造 --#region -- 构造 --
InBlock.gif        
public MyRadioButtonList()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-- 属性 --#region -- 属性 --
InBlock.gif        
public string AlternatingItemCssClass
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn alternatingItemCssClass; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ alternatingItemCssClass = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string ItemCssClass
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn itemCssClass; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ itemCssClass = value;}
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-- 重写呈现方法 --#region -- 重写呈现方法 --
InBlock.gif        
protected override void Render(HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MyRepeatInfo info 
= new MyRepeatInfo();
InBlock.gif            Style cStyle 
= (ControlStyleCreated ? ControlStyle : null);
InBlock.gif            info.RepeatColumns 
= RepeatColumns;
InBlock.gif            info.RenderRepeater(writer,
this,cStyle,this,itemCssClass,alternatingItemCssClass);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class MyRepeatInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-- 成员变量 --#region -- 成员变量 --
InBlock.gif        
InBlock.gif        
int    repeatColumns                = 0;
InBlock.gif        RepeatDirection    direction        
= RepeatDirection.Vertical;
InBlock.gif 
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-- 构造 --#region -- 构造 --
InBlock.gif        
public MyRepeatInfo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-- 属性 --#region -- 属性 --
InBlock.gif        
public int RepeatColumns
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn repeatColumns; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ repeatColumns = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public RepeatDirection Direction
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn direction; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ direction = value;}
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-- 方法 --#region -- 方法 --
InBlock.gif        
public void RenderRepeater(HtmlTextWriter writer,IRepeatInfoUser user,Style controlStyle,WebControl baseControl)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RenderRepeater(writer,user,controlStyle,baseControl,
null,null);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void RenderRepeater(HtmlTextWriter writer,IRepeatInfoUser user,Style controlStyle,WebControl baseControl,string alternatingItemCssClass)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RenderRepeater(writer,user,controlStyle,baseControl,
null,alternatingItemCssClass);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void RenderRepeater(HtmlTextWriter writer,IRepeatInfoUser user,Style controlStyle,WebControl baseControl,string itemCssClass,string alternatingItemCssClass)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
switch (direction)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case RepeatDirection.Horizontal:
InBlock.gif                    RenderHorizontal(writer,user,controlStyle,baseControl,itemCssClass,alternatingItemCssClass);
InBlock.gif                    
break;
InBlock.gif                
case RepeatDirection.Vertical:
InBlock.gif                    RenderVertical(writer,user,controlStyle,baseControl,itemCssClass,alternatingItemCssClass);
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-- 辅助方法 --#region -- 辅助方法 --
InBlock.gif        
void RenderHorizontal(HtmlTextWriter writer,IRepeatInfoUser user,Style controlStyle,WebControl baseControl,string itemCssClass,string alternatingItemCssClass)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//水平
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
//->
InBlock.gif
        void RenderVertical(HtmlTextWriter writer,IRepeatInfoUser user,Style controlStyle,WebControl baseControl,string itemCssClass,string alternatingItemCssClass)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//垂直--  
InBlock.gif
            int total = user.RepeatedItemCount;
InBlock.gif            
int colsCount = repeatColumns;
InBlock.gif
InBlock.gif            
if(colsCount == 0)
InBlock.gif                colsCount 
= 1;
InBlock.gif            
InBlock.gif            WebControl ctrl 
= new Table();
InBlock.gif
InBlock.gif            ctrl.ID 
= baseControl.ClientID;
InBlock.gif            ctrl.CopyBaseAttributes(baseControl);
InBlock.gif            ctrl.ApplyStyle(controlStyle);
InBlock.gif
InBlock.gif            
string itemCss;
InBlock.gif
InBlock.gif            ctrl.RenderBeginTag(writer);
InBlock.gif
InBlock.gif            
for ( int index = 0; index < total ; index ++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(index % colsCount == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if((index / colsCount) % 2 == 0)
InBlock.gif                        itemCss 
= itemCssClass;
InBlock.gif                    
else
InBlock.gif                        itemCss 
= alternatingItemCssClass;
InBlock.gif
InBlock.gif                    writer.AddAttribute(HtmlTextWriterAttribute.Class,itemCss);
InBlock.gif                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
ExpandedSubBlockEnd.gif                }

InBlock.gif                writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif                RepeatInfo info 
= new RepeatInfo();
InBlock.gif                info.RepeatColumns 
= repeatColumns;
InBlock.gif                user.RenderItem(ListItemType.Item,index,info,writer);
InBlock.gif                writer.RenderEndTag();
InBlock.gif
InBlock.gif                
if(((index+1% colsCount == 0|| (index+1 == total))
InBlock.gif                    writer.RenderEndTag();
InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            ctrl.RenderEndTag(writer);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/shichao/archive/2004/11/17/64837.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值