asp:RadioButton或asp:RadioButtonList

时间:2015-12-01 11:53:07

标签: asp.net vb.net

我正在寻找一个明智的意见,说明我应该以何种方式为客户提供解决方案。

解决方案涉及在asp:Repeater中使用单选按钮。最初我选择使用asp:RadioButtonList,但我遇到的问题是它坚持提供文本标签,当使用DataSourceID时我无法用label标签封装radiobutton标签,从而阻止我提供更大的点击区域任何使用触摸屏设备的人。

它呈现如下:

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

我想要的是:

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

我可以通过在标签标签中包装asp:RadioButton来实现上述目的。但是后来找出GroupName中选择了哪个单选按钮意味着我需要通过每个单选按钮并检查其检查属性。不是很优雅。

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

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

或者只是选择简单的方法,删除asp:RadioButtonList并迭代每个asp:RadioButton,直到找到已检查的radiobutton。

我的所有想法&#39;看起来过于复杂而且不是特别优雅。有没有人有类似问题的经验?

1 个答案:

答案 0 :(得分:0)

我最终使用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>

正如我在原始问题中所述,radiobutton包含在asp:Repeater中。为了遍历radiobuttons和每个表行,我使用了以下代码:

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

希望人们觉得这很有用。