asp:RadioButton or asp:RadioButtonList



我正在为客户寻求一个明智的解决方案。

解决方案包括在asp:Repeater中使用单选按钮。最初我选择使用asp:RadioButtonList,但我遇到的问题是,当使用DataSourceID时,它坚持提供文本标签,并且我无法用标签标签封装单选按钮标签,因此无法为任何使用触摸屏设备的人提供更大的点击区域。

它的渲染图如下:

<td>
  <input />
  <label />
</td>

我想要的是:

<td>
  <label>
    <input />
  </label>
</td>

我可以通过在标签标签中包装asp:RadioButton来实现上述功能。但是,发现GroupName中的哪个单选按钮被选中意味着我需要遍历每个单选按钮并检查其选中的属性。不是很优雅。

我已经对这个主题做了一些研究,看起来我可以使用CSS来移动asp:RadioButtonList单选按钮的位置,这样看起来它们就包含在标签中,并删除标签文本。

从asp:RadioButtonList中删除DataSourceID将完全删除<label>标记。所以我可以在每个单选按钮周围插入一个标签标签。

或者只采用简单的方法,删除asp:RadioButtonList并遍历每个asp:RadioButton,直到找到选中的单选按钮。

我所有的"想法"似乎都过于复杂,并不是特别优雅。有人经历过类似的问题吗?

我最终使用了RadioButtons,使用了以下代码。

<td>
  <label>
    <asp:RadioButton ID="rb1" runat="server" />
  </label>
</td>
<td>
  <label>
    <asp:RadioButton ID="rb2" runat="server" />
  </label>
</td>
<td>
  <label>
    <asp:RadioButton ID="rb3" runat="server" />
  </label>
</td>

正如我在最初的问题中所说,单选按钮包含在asp:Repeater中。为了遍历单选按钮和每个表行,我使用了以下代码:

For Each item As RepeaterItem In RepeaterName.Items
  If item.ItemType <> ListItemType.Header And item.ItemType <> ListItemType.Footer And item.ItemType <> ListItemType.Separator Then
  Dim listOfNames() As String = {"rb1", "rb2", "rb3"}  
  Dim attended As Boolean = False
  Dim index As Integer = 0
  Do While checked = False
    Dim rbAttendanceCode As RadioButton = CType(item.FindControl(listOfNames(index).ToString), RadioButton)
    If rbAttendanceCode.Checked Then
        'Do whatever you want with the checked radiobutton    
        'set the flag as true so we stop looping through the radio buttons.
        checked = True
      End If
      index += 1
    Loop
  End If
Next

希望人们觉得这很有用。

最新更新