Joh*_*ski 15 c# asp.net validation radiobuttonlist
我在用:
if (RadioButtonList_VolunteerType.SelectedItem != null)
Run Code Online (Sandbox Code Playgroud)
或怎么样:
if (RadioButtonList_VolunteerType.Index >= 0)
Run Code Online (Sandbox Code Playgroud)
或者怎么样(根据Andrew Hare的回答):
if (RadioButtonList_VolunteerType.Index > -1)
Run Code Online (Sandbox Code Playgroud)
对于那些可能阅读此问题的人,以下不是有效的方法.正如Keltex指出的那样,所选值可能是一个空字符串.
if (string.IsNullOrEmpty(RadioButtonList_VolunteerType.SelectedValue))
Run Code Online (Sandbox Code Playgroud)
And*_*are 11
这些都是检查所选值的有效且完全合法的方法.我个人觉得
RadioButtonList_VolunteerType.SelectedIndex > -1
Run Code Online (Sandbox Code Playgroud)
是最清楚的.
在可读性方面,他们都缺乏适合我的东西.这似乎是扩展方法的一个很好的候选者.
public static class MyExtenstionMethods
{
public static bool HasSelectedValue(this RadioButtonList list)
{
return list.SelectedItem != null;
}
}
...
if (RadioButtonList_VolunteerType.HasSelectedValue)
{
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35916 次 |
最近记录: |