asp.net - 如何判断一个控件是复选框还是单选按钮?

标签 asp.net vb.net

我有一个 if 语句,需要检查传递的控件是复选框还是单选按钮

            If TypeOf (Control) Is CheckBox Then

                If count = 1 Then
                    text += Me.GetCheckBoxValueJQuerySyntax(Control.ClientID) & " + '~' + "
                Else
                    text += Me.GetCheckBoxValueJQuerySyntax(Control.ClientID) & "'~' + "
                End If

            End If
            If TypeOf (Control) Is RadioButton Then

                If CType(Control, RadioButton).GroupName <> "" Then

                    If Not Me._GroupNameArrayList.Contains(CType(Control, RadioButton).GroupName) Then

                        Me._GroupNameArrayList.Add(CType(Control, RadioButton).GroupName)

                        If count = 1 Then
                            text += Me.GetRadioValueJQuerySyntax(Control.ClientID) & " + '~' + "
                        Else
                            text += Me.GetRadioValueJQuerySyntax(Control.ClientID) & "'~' + "
                        End If
                    End If
                Else

                    If count = 1 Then
                        text += Me.GetRadioValueJQuerySyntax(Control.ClientID) & " + '~' + "
                    Else
                        text += Me.GetRadioValueJQuerySyntax(Control.ClientID) & "'~' + "
                    End If

                End If


            End If

这会失败,因为复选框和重播按钮派生自同一类,所以我如何检查控件是复选框还是单选按钮

最佳答案

你已经很接近了; RadioButton 派生自 CheckBox,因此从技术上讲,它们都是 CheckBox。因此,在这种情况下,您可以翻转语句并使用 Else If 来获取您要查找的结果。

If TypeOf Control Is RadioButton Then
    ' The control is a RadioButton.
ElseIf TypeOf Control Is CheckBox Then
    ' The control is a CheckBox.
End If

关于asp.net - 如何判断一个控件是复选框还是单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8436137/

相关文章:

c# - 具有 "&"的查询字符串值

asp.net <form> 标记从 asp 中剥离 :literal

c# - 什么是NullReferenceException,我该如何解决?

mysql - 类型 'MySqlConnection' 未定义

asp.net - webcal URI 方案上的 ics 文件

asp.net - Entity Framework 不会在现有数据库上创建身份表

javascript - 是否可以一次动态添加 2 行?

mysql 按 desc 排序并忽略字母表

c# - XML 文档中的多语言示例

vb.net - 使用 ASP :RadioButton 触发 OnCheckedChanged 事件