在单选按钮组上选择案例可以吗?
我有 4 个 asp.net 单选按钮,它们被分组,我想找出选中了哪一个。
看起来很自然的做法是:
Select Case RadioButton.Checked = True
Case myRadioButton1
Case myRadioButton2
Case myRadioButton3
Case Else
End Select
我刚刚收到“引用非共享成员引用”错误。很遗憾,因为这似乎是进行此测试的一种干净方法。这可能吗?
I have say 4 asp.net radiobuttons which are grouped, I want to find out which one is checked.
What seems natural to do is:
Select Case RadioButton.Checked = True
Case myRadioButton1
Case myRadioButton2
Case myRadioButton3
Case Else
End Select
I just get a 'reference to a non-shared member reference' error. It's a shame because it seems such a clean way to do this test.. Is it possible??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不可以。RadioButton 类的 Checked 属性是非共享的,因此除非有实例,否则无法获取其值。您可以使用 ElseIf 链来完成此操作,但最干净的解决方案 IMO 是使用 RadioButtonList,这将允许您动态添加项目并在一次调用中读取所选项目,就像使用 DropdownList 一样。
No. The Checked property of class RadioButton is non-shared, so you cannot get its value unless you have an instance. You can do this using an ElseIf chain, but the cleanest solution IMO is to use a RadioButtonList instead, which will allow you to add items dynamically and read the selected item in one call, just like with a DropdownList.
您可以使用 RadioButtonList 代替。
您只能检查 select/case 中的原始数据类型。使用普通的单选按钮,您只能在处理所有复选框时检查 CheckedChanged 事件中的引用。例如:
但这无论如何都不是你的问题。正如我上面提到的那样,使用 RadiobuttonList 或在选中状态下使用 If/else,因为它更自然。
You could use a RadioButtonList instead.
You can only check for primitive data-types in select/case. With normal RadioButtons you could only check for a reference in the CheckedChanged Event when all CheckBoxes are handled there. For example:
But that was not your question anyway. Use a RadiobuttonList instead as i mentioned above or a If/else on checked-state because it is more natural.