Asp.net 如何确定控件是复选框还是单选按钮?
我有一个if语句,需要检查传递的控件是复选框还是单选按钮Asp.net 如何确定控件是复选框还是单选按钮?,asp.net,vb.net,Asp.net,Vb.net,我有一个if语句,需要检查传递的控件是复选框还是单选按钮 If TypeOf (Control) Is CheckBox Then If count = 1 Then text += Me.GetCheckBoxValueJQuerySyntax(Control.ClientID) & " + '~' + " Else
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
如果TypeOf(Control)为复选框,则
如果计数=1,则
text+=Me.GetCheckBoxValueJQuerySyntax(Control.ClientID)&“+”~“+”
其他的
text+=Me.GetCheckBoxValueJQuerySyntax(Control.ClientID)&“~'+”
如果结束
如果结束
如果(控制)类型为单选按钮,则
如果CType(控件,RadioButton).GroupName“”则
如果不是我._groupnamearaylist.Contains(CType(Control,RadioButton).GroupName),那么
Me.\u GroupNameArrayList.Add(CType(控件,RadioButton).GroupName)
如果计数=1,则
text+=Me.GetRadioValueJQuerySyntax(Control.ClientID)&“+”~“+”
其他的
text+=Me.GetRadioValueJQuerySyntax(Control.ClientID)&“~'+”
如果结束
如果结束
其他的
如果计数=1,则
text+=Me.GetRadioValueJQuerySyntax(Control.ClientID)&“+”~“+”
其他的
text+=Me.GetRadioValueJQuerySyntax(Control.ClientID)&“~'+”
如果结束
如果结束
如果结束
这会失败,因为复选框和重拨按钮来自同一个类,所以我如何检查控件是复选框还是单选按钮
RadioButton
源于CheckBox
,因此从技术上讲,它们都是CheckBox
es。因此,在本例中,您可以翻转语句并使用Else 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
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特有的
Private Sub Button3_Click(sender As System.Object, _
e As System.EventArgs) Handles Button3.Click
Dim ctrl As Control = Me.GetNextControl(Me, True)
Do Until ctrl Is Nothing
If TypeOf ctrl Is RadioButton Then
Debug.WriteLine("rb " & ctrl.Name)
ElseIf TypeOf ctrl Is CheckBox Then
Debug.WriteLine("cb " & ctrl.Name)
End If
'reverse check
If TypeOf ctrl Is CheckBox Then
Debug.WriteLine("cb " & ctrl.Name)
ElseIf TypeOf ctrl Is RadioButton Then
Debug.WriteLine("rb " & ctrl.Name)
End If
ctrl = Me.GetNextControl(ctrl, True)
Loop
End Sub
看起来顺序无关紧要,但我没有使用asp.net进行测试。不,您的答案将检查
复选框作为第一个If
语句,检查单选按钮作为第二个(单独的)If
语句。我的检查顺序颠倒了(很重要),并且使用了ElseIf
语句而不是第二个If
语句(也很重要)。它仍然以错误的顺序进行检查,这仍然会给出错误的结果<必须首先选中code>RadioButton
,因为它是相关类的更具体派生。在Windows窗体中,CheckBox
和RadioButton
都不会相互继承。在ASP.NET中,RadioButton
从复选框中插入。您的示例可以在Windows窗体中运行,但第二个If
语句会在ASP.NET中给出错误的结果。请随时学习新内容。