【问题标题】:asp:RadioButton or asp:RadioButtonListasp:RadioButton 或 asp:RadioButtonList
【发布时间】:2015-12-01 11:53:07
【问题描述】:

我正在寻找关于我应该以何种方式为客户提供解决方案的知情意见。

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

呈现如下:

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

我想要的是:

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

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

我对该主题进行了一些研究,看起来我可以使用 CSS 来移动 asp:RadioButtonList 单选按钮的位置,使其看起来像是包含在标签中并删除标签文本。

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

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

我所有的“想法”似乎都过于复杂而且不是特别优雅。有没有人遇到过类似的问题?

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

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

    希望人们觉得这很有用。

    【讨论】:

      猜你喜欢
      • 2013-12-10
      • 2014-05-16
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 2015-12-11
      • 2019-08-24
      相关资源
      最近更新 更多