【问题标题】:Foreach RadioButtons in ASP.NET WebformsASP.NET Web 窗体中的 Foreach 单选按钮
【发布时间】:2017-04-22 17:01:37
【问题描述】:

我有ASP MultiView 的申请表。在其中一个视图中,我有几个单选按钮如下--

<div class="row">
  <asp:CustomValidator ID="Group1Validator" GroupName="Group1" runat="server"
   ValidationGroup="Group1" Display="Dynamic"></asp:CustomValidator>
  <asp:RadioButton ID="Group1Yes" runat="server" GroupName="Group1" Text="Yes"/>
  <asp:RadioButton ID="Group1No" runat="server" GroupName="Group1" Text="No" />
</div>

现在在后面的代码中,我想遍历该页面中的所有RadioButtons,并将值保存在DB 中。我已经尝试了HERELoopLINQ 版本的解决方案,但问题是我的 RadioButton 控件没有直接在页面中。让我详细说明-

首先,我在一个页面中有多个视图-

多视图包含视图-

最后在视图内部我有 RadioButton 控件-

现在我的目标是获取所有被选中的RadioButton 的值,这些值是使用上面的div 布局的。

任何帮助和指导将不胜感激!谢谢!

【问题讨论】:

    标签: c# asp.net webforms


    【解决方案1】:

    您必须将 RadioButtons 作为 RadioButtonList 的子项,然后在 foreach 循环中迭代 RadioButtonList 项。

    【讨论】:

      【解决方案2】:

      为什么不使用RadioButtonList

      <asp:RadioButtonList ID="RadioButtonList1" runat="server">
          <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
          <asp:ListItem Text="No" Value="0"></asp:ListItem>
      </asp:RadioButtonList>
      

      然后你可以在后面的代码中获取值

      string value = RadioButtonList1.SelectedValue;
      

      您还可以在后面的代码中向 RadioButtonList 添加项目

      RadioButtonList1.Items.Insert(0, new ListItem("Yes", "1", true));
      RadioButtonList1.Items.Insert(1, new ListItem("No", "0", true));
      

      【讨论】:

        猜你喜欢
        • 2013-11-21
        • 2011-12-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 2021-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多