Tip/Trick: ASP.NET 2.0 Deal with DBNull value when bind to RadioButtonList/当绑定RadioButtonL
2005-01-01 00:00
639 查看
对于输入值二选一这种情况(比如是、否,男、女),UI上最符合逻辑得输入控件应该时单选按纽组(radio),当然也可以用下拉列表框(select),但我觉得多选一的时候用它才合适。在ASP.NET 2.0里,使用单选按纽组对应的服务器控件就是RadioButtonList。绑定数据时与其他ListControl一样设置其SelectedValue属性SelectedValue='<%# Bind("FiledlNameInDB") %>'(这里谈的是直接绑定数据库,通常时通过Datasrouce控件)。但这时有个问题,当数据库中该字段允许为空,而用户确实输入了空值时,当执行绑定时RadioButtonList就会报错,因为在值列表中找不到对应的一个空值。DripdownList也有这个问题,但Dropdownlist可以简单通过加入一个具有空值的Listitem来解决这个问题,如<asp:ListItem Value="">--None--</asp:ListItem>。对于RadioButtonList尝试了几种办法,没有什么好的方案。
觉得最彻底的应该是重写RadioButtonList 的SelectedValue属性,当set该属性时如果在值列表里未查到值则忽略而不是报错。另一个权益之计就是仿造DropdownList的解决方法,但要麻烦的多,在DataBinding事件之前添加一个具有空值的item,然后在render之前在去掉该item。
<asp:RadioButtonList ID="RadioButtonList1" runat="server" SelectedValue='<%# Bind("FiledlNameInDB") %>' RepeatDirection="Horizontal" RepeatLayout="Flow" OnInit="RadioButtonList1_Init" OnPreRender="RadioButtonList1_PreRender"
<asp:ListItem Value="True">YES</asp:ListItem>
<asp:ListItem Value="False">NO</asp:ListItem>
</asp:RadioButtonList>
觉得最彻底的应该是重写RadioButtonList 的SelectedValue属性,当set该属性时如果在值列表里未查到值则忽略而不是报错。另一个权益之计就是仿造DropdownList的解决方法,但要麻烦的多,在DataBinding事件之前添加一个具有空值的item,然后在render之前在去掉该item。
<asp:RadioButtonList ID="RadioButtonList1" runat="server" SelectedValue='<%# Bind("FiledlNameInDB") %>' RepeatDirection="Horizontal" RepeatLayout="Flow" OnInit="RadioButtonList1_Init" OnPreRender="RadioButtonList1_PreRender"
<asp:ListItem Value="True">YES</asp:ListItem>
<asp:ListItem Value="False">NO</asp:ListItem>
</asp:RadioButtonList>
protected void RadioButtonList1_Init(object sender, EventArgs e)
{
RadioButtonList rbl = sender as RadioButtonList;
rbl.Items.Add(new ListItem(string.Empty));
}
protected void RadioButtonList1_PreRender(object sender, EventArgs e)
{
RadioButtonList rbl = sender as RadioButtonList;
rbl.Items.Remove(string.Empty);
}
{
RadioButtonList rbl = sender as RadioButtonList;
rbl.Items.Add(new ListItem(string.Empty));
}
protected void RadioButtonList1_PreRender(object sender, EventArgs e)
{
RadioButtonList rbl = sender as RadioButtonList;
rbl.Items.Remove(string.Empty);
}
相关文章推荐
- Tip/Trick: ASP.NET 2.0 Deal with DBNull value when bind to RadioButtonList/当绑定RadioButtonL
- ASP.NET 2.0 Language Swithcer and Theme Swicher 多语言转换和多样式主题转换
- ASP.NET 2.0 Menu Control: set static item selected when select its child dynamic items/选择men
- Efficient Data Paging and Sorting with ASP.NET 2.0 and SQL 2005
- Cool Tips and Tricks with ASP.NET 2.0 posted by Scott
- ASP.NET 2.0 Language Swithcer and Theme Swicher 多语言转换和多样式主题转换
- Cool Tips and Tricks with ASP.NET 2.0 posted by Scott
- Efficient Data Paging and Sorting with ASP.NET 2.0 and SQL 2005
- Great New Advanced Article on ASP.NET 2.0 Master Pages
- ASP.NET 2.0:通过SqlDataSource绑定数据到普通控件
- Working with Web Resources in ASP.NET 2.0
- ASP.NET 2.0: URL Mapping with RegEx Support
- Tip/Trick ASP.NET 2.0: DropDownList DataBind
- Pointers to Great ASP.NET Atlas Content
- Supporting Templates with ASP.NET User Controls
- Tip/Trick ASP.NET 2.0: DropDownList DataBind
- Atlas Control Toolkit and Source Code for the Build-in Asp.Net 2.0 Providers
- ASP.NET Tip: Using RadioButton Controls in a Repeater
- Tip/Trick: Url Rewriting with ASP.NET 【中文版】
- ASP.NET 2.0 Localization (Video, Whitepaper, and Database Provider Support)