查看RadioButtonList是否具有选定值的最佳方法是什么? [英] What is the best way to see if a RadioButtonList has a selected value?

查看:69
本文介绍了查看RadioButtonList是否具有选定值的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用:

if (RadioButtonList_VolunteerType.SelectedItem != null)

或如何:

if (RadioButtonList_VolunteerType.Index >= 0)

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

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

if (RadioButtonList_VolunteerType.Index > -1)

对于可能阅读此问题的人来说,以下无效方法.正如Keltex指出的那样,所选值可能是一个空字符串.

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))

推荐答案

在可读性方面,它们都缺少我的东西.这似乎是扩展方法的不错选择.

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
}

这篇关于查看RadioButtonList是否具有选定值的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆