查看 RadioButtonList 是否具有选定值的最佳方法是什么?

发布于 2024-07-16 15:24:33 字数 461 浏览 9 评论 0原文

我正在使用:

if (RadioButtonList_VolunteerType.SelectedItem != null)

或怎么样:

if (RadioButtonList_VolunteerType.Index >= 0)

或怎么样(根据安德鲁·黑尔的回答):

if (RadioButtonList_VolunteerType.Index > -1)

对于那些可能阅读此问题的人,以下不是有效的方法。 正如 Keltex 指出的,所选值可以是空字符串。

if (string.IsNullOrEmpty(RadioButtonList_VolunteerType.SelectedValue))

I am using:

if (RadioButtonList_VolunteerType.SelectedItem != null)

or how about:

if (RadioButtonList_VolunteerType.Index >= 0)

or how about (per Andrew Hare's answer):

if (RadioButtonList_VolunteerType.Index > -1)

To those who may read this question, the following is not a valid method. As Keltex pointed out, the selected value could be an empty string.

if (string.IsNullOrEmpty(RadioButtonList_VolunteerType.SelectedValue))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风筝在阴天搁浅。 2024-07-23 15:24:33

这些都是检查选定值的有效且完全合法的方法。 我个人认为

RadioButtonList_VolunteerType.SelectedIndex > -1

是最清楚的。

Those are all valid and perfectly legitimate ways of checking for a selected value. Personally I find

RadioButtonList_VolunteerType.SelectedIndex > -1

to be the clearest.

爱要勇敢去追 2024-07-23 15:24:33

就可读性而言,它们对我来说都缺乏一些东西。 这似乎是扩展方法的一个很好的候选者。

public static class MyExtenstionMethods 
{   
  public static bool HasSelectedValue(this RadioButtonList list) 
  {
    return list.SelectedItem != null;
  }
}


...

if (RadioButtonList_VolunteerType.HasSelectedValue)
{
 // do stuff
}

In terms of readability they all lack something for me. This seems like a good candidate for an extension method.

public static class MyExtenstionMethods 
{   
  public static bool HasSelectedValue(this RadioButtonList list) 
  {
    return list.SelectedItem != null;
  }
}


...

if (RadioButtonList_VolunteerType.HasSelectedValue)
{
 // do stuff
}
缱倦旧时光 2024-07-23 15:24:33

问题更多地围绕是否检查 null 或检查 int 值。 Martin 伟大的扩展方法也可以这样写:

public static bool HasSelectedValue(this ListControl list)
{
    return list.SelectedIndex >= 0;
}

ListControl 的 MSDN 文档指出:

SelectedItem 的默认值为 null .

SelectedIndex 的默认值为 - 1.

所以这两种方法都是有效的,而且都有效。 问题是哪一个是最好的方法。 我猜测 SelectedIndex 因为它是值类型操作而不是引用类型操作。 但我没有任何东西可以支持这一点。

The question revolves more around whether to check for null or check value of an int. Martin's great extension method could also be written:

public static bool HasSelectedValue(this ListControl list)
{
    return list.SelectedIndex >= 0;
}

The MSDN documentation for a ListControl states:

Default for SelectedItem is null.

Default for SelectedIndex is -1.

So either are valid ways and both work. The question is which is the best way. I'm guessing SelectedIndex as it is a value type operation rather than reference type operation. But I don't have anything to back that up with.

梅倚清风 2024-07-23 15:24:33

我建议:

RadioButtonList_VolunteerType.SelectedIndex>=0. 

根据 Microsoft 文档

列表中所选项目的最低序数索引。 默认值为-1,表示未选择任何内容。

string.IsNullOrEmpty(RadioButtonList_VolunteerType.SelectedValue) 并不总是有效,因为您可以拥有一个具有空值的 ListItem:

<asp:ListItem Value=''>This item has no value</asp:ListItem>

I recommend:

RadioButtonList_VolunteerType.SelectedIndex>=0. 

According to the Microsoft Documentation:

The lowest ordinal index of the selected items in the list. The default is -1, which indicates that nothing is selected.

string.IsNullOrEmpty(RadioButtonList_VolunteerType.SelectedValue) will not always work as you can have a ListItem with an empty value:

<asp:ListItem Value=''>This item has no value</asp:ListItem>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文