在单选按钮组上选择案例可以吗?

发布于 2024-09-12 11:10:44 字数 299 浏览 1 评论 0原文

我有 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 技术交流群。

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

发布评论

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

评论(3

我纯我任性 2024-09-19 11:10:44
Select Case True

  Case RadioButton1.Checked

  Case RadioButton2.Checked

  Case RadioButton3.Checked

  Case Else

End Select
Select Case True

  Case RadioButton1.Checked

  Case RadioButton2.Checked

  Case RadioButton3.Checked

  Case Else

End Select
你丑哭了我 2024-09-19 11:10:44

不可以。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.

靑春怀旧 2024-09-19 11:10:44

您可以使用 RadioButtonList 代替。
您只能检查 select/case 中的原始数据类型。使用普通的单选按钮,您只能在处理所有复选框时检查 CheckedChanged 事件中的引用。例如:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler Me.RadioButton1.CheckedChanged, AddressOf CheckedChanged
        AddHandler Me.RadioButton2.CheckedChanged, AddressOf CheckedChanged
    End Sub

    Private Sub CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
       If sender Is Me.RadioButton1 Then

        ElseIf sender Is Me.RadioButton2 Then
            '........
        End If
    End Sub

但这无论如何都不是你的问题。正如我上面提到的那样,使用 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:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler Me.RadioButton1.CheckedChanged, AddressOf CheckedChanged
        AddHandler Me.RadioButton2.CheckedChanged, AddressOf CheckedChanged
    End Sub

    Private Sub CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
       If sender Is Me.RadioButton1 Then

        ElseIf sender Is Me.RadioButton2 Then
            '........
        End If
    End Sub

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.

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