CheckBoxList、DropDownList、ListBox、RadioButtonList、BulletedList对于&符号的解释区别

这几个控件都继承自ListControl,功能想必大家都很清楚了。但是这几个控件对于&符号的解释还是有区别的。先看下代码:

 
  
protected void Page_Load( object sender, EventArgs e)
{
List
< Person > PersonList = new List < Person > () { new Person( 1 , " 张&三 " ),
new
Person( 1 , " 李&nbsp;四 " ), new Person( 1 , " 王五 " ), new Person( 1 , " 赵六 " ) };
CheckBoxList1.DataSource
= PersonList;
CheckBoxList1.DataTextField
= " Name " ;
CheckBoxList1.DataValueField
= " Id " ;
CheckBoxList1.DataBind();

DropDownList1.DataSource
= PersonList;
DropDownList1.DataTextField
= " Name " ;
DropDownList1.DataValueField
= " Id " ;
DropDownList1.DataBind();

ListBox1.DataSource
= PersonList;
ListBox1.DataTextField
= " Name " ;
ListBox1.DataValueField
= " Id " ;
ListBox1.DataBind();

RadioButtonList1.DataSource
= PersonList;
RadioButtonList1.DataTextField
= " Name " ;
RadioButtonList1.DataValueField
= " Id " ;
RadioButtonList1.DataBind();

BulletedList1.DataSource
= PersonList;
BulletedList1.DataTextField
= " Name " ;
BulletedList1.DataValueField
= " Id " ;
BulletedList1.DataBind();



}

效果:

2011030117354954.jpg

可以发现DropDownList、ListBox、BulletedList中Item的内容在转换成Html code的时候 主动的把"&"编程"&amp;"了。而RaddioButtonList和CheckBoxList却没有转移"&"。

查看它们继承的接口发现 前三个只继承了ListControl, IPostBackDataHandler,后两者继承了 ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler

INamingContainer:标识在 Page 对象的控件层次结构内创建新 ID 命名空间的容器控件。任何实现该接口的控件都创建一个新的命名空间,在这个新的命名空间中,所有子控件 ID 属性在整个应用程序内保证是唯一的。由该接口提供的标记允许在支持数据绑定的 Web 服务器控件内唯一命名动态生成的服务器控件实例。

IRepeatInfoUser:定义必须由任何重复项列表的列表控件实现的属性和方法。IRepeatInfoUser 接口定义必须由所有重复项列表的列表控件(如 CheckBoxListDataListRadioButtonList)实现的属性和方法。

看了两个接口发现 可能跟IRepeatInfoUser有关。再看IRepeatInfoUser发现里面有个方法:RenderItem——用指定的信息呈现列表中的项。

void RenderItem(
ListItemType itemType,
int repeatIndex,
RepeatInfo repeatInfo,
HtmlTextWriter writer
)
itemType:
ListItemType 枚举值之一。
repeatIndex:指定列表控件中项的位置的序号索引
repeatInfo:表示用于呈现列表中的项的信息
writer:表示要在客户端呈现 HTML 内容的输出流(貌似跟这个HtmlTextWriter有关)

本人没有用Reflector看后台的代码是怎么写的,但是感觉应该在这里面没有对"&"做转义。呵呵,纯粹自己猜测,如果不对还希望高手指正。
 
    
 
    

转载于:https://www.cnblogs.com/SandyWang/archive/2011/03/01/1968222.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值