如何检查单选按钮是否在组中被选中
我有一组单选按钮,每组有 5 - 27 个单选按钮。如果选中了组中的任何单选按钮,我会将 1 存储在数据库中,否则会存储 0。现在,我使用 if 循环检查每个单选按钮,以查看它们是否已选中并设置数据库值。我也在尝试使用下面的代码。是否有一个好的/更好的方法来检查它们是否被检查?
当前代码:
'rname is radiobutton prefix for a given group
'cnt is number of radiobuttons in the group
Private Function RadioIsChecked(ByVal rname As String, ByVal cnt As Integer) As Integer
Dim retval As Integer = 0
For i = 0 To cnt - 1
Dim rdbName As String = rname & i
Dim rdb As New RadioButton()
rdb = CType(Me.Page.FindControl(rdbName), RadioButton)
If rdb.Checked Then
retval = 1
End If
Next
Return retval
End Function
注意:我无法使用单选按钮列表。我知道使用它可以轻松实现这一点,但我想获得单选按钮的解决方案
I have group of radio buttons, each group ranges from 5 - 27 radiobuttons. And if any radio button in a group is checked I store 1 in db else I store 0. Now I'm checking each radiobutton using if loop to see if they are checked and set database value. I'm also trying to use the code below. Is there a good/better approach to check if they are checked or not?
Current code:
'rname is radiobutton prefix for a given group
'cnt is number of radiobuttons in the group
Private Function RadioIsChecked(ByVal rname As String, ByVal cnt As Integer) As Integer
Dim retval As Integer = 0
For i = 0 To cnt - 1
Dim rdbName As String = rname & i
Dim rdb As New RadioButton()
rdb = CType(Me.Page.FindControl(rdbName), RadioButton)
If rdb.Checked Then
retval = 1
End If
Next
Return retval
End Function
Note: I cannot use radio button list. I know this can be achieved easily using this but i want to get solution for radiobutton
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否可以使用单选按钮列表
,请参阅: http://www .java2s.com/Code/ASP/Asp-Control/GetselecteditemvaluefromaspradiobuttonlistVBnet.htm
Is it possible for you to use a radiobuttonlist
see: http://www.java2s.com/Code/ASP/Asp-Control/GetselecteditemvaluefromaspradiobuttonlistVBnet.htm
无论哪种方式,都取决于你。
只有在找到选中的项目后退出迭代,您才会获得所需的性能提升。
您可以按如下方式修改迭代:
Either way, it's up to you.
You'll get the needed performance boost only if you exit the iteration after finding the checked item.
You can modify the iteration as follows:
您还可以使用
Request.Form("GroupNameGoesHere")
获取该组中当前选定的单选按钮的值(如果不存在,则获取空字符串)。You can also use
Request.Form("GroupNameGoesHere")
to get the value of the currently selected radio button in that group (or an empty string if none exist).将其改为 RadioButtonList。然后您可以简单地检查列表的“SelectedItem”属性。
看看 http://www.startvbdotnet.com/aspsite/controls/rblist.aspx< /a> 为例。
Make it a RadioButtonList instead. Then you can simply check the "SelectedItem" property of the List.
Look at http://www.startvbdotnet.com/aspsite/controls/rblist.aspx for examples.
如果您使用如上所述的单选按钮列表,您可以执行类似的操作。测试是否选择了某些内容。
If you use a radiobutton list as mentioned you could do something like this. To test if something was selected.