【问题标题】:How can I get selected values from radio button list and check box list?如何从单选按钮列表和复选框列表中获取选定的值?
【发布时间】:2017-11-29 08:28:27
【问题描述】:

我正在尝试从 asp.net、C# 的单选按钮列表和复选框列表中获取选定的值。 这是我的代码。

aspx.cs

protected void Next_Click(object sender, EventArgs e)
    {
        var retList = new List<string>();

        foreach (RepeaterItem item in questionRepeater.Items)
        {
            // Checking the item is a data item
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {

                var rdbList = item.FindControl("RadioButtonList1") as RadioButtonList;
                // Get the selected value
                if (rdbList != null)
                {
                    retList.Add(rdbList.SelectedValue);
                }
            }
        }


        foreach (RepeaterItem item in questionRepeater.Items)
        {
            // Checking the item is a data item
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                var rdbList = item.FindControl("CheckBoxList1") as CheckBoxList;
                // Get the selected value
                if (rdbList != null)
                {
                    retList.Add(rdbList.SelectedValue);
                }
            }



        }
    }

aspx

<asp:Repeater ID="questionRepeater" runat="server">
<ItemTemplate>
     <div runat="server" visible='<%# (Eval("QUESTION_TYPE").ToString() == 
     "0") %>'> 
      <table style="width:100%; table-layout: fixed;">  
                <asp:RadioButtonList id="RadioButtonList1" CellSpacing="50" 
      TextAlign="Left" runat="server" 
      DataSource='<%#GetChild(Container.DataItem,"ChoicesRelate") %>' 
      DataTextField="CHOICES_CONTENT" DataValueField="CHOICES_NO">
                </asp:RadioButtonList>
           </table> 
     </div>

     <div runat="server" visible='<%# (Eval("QUESTION_TYPE").ToString() == 
     "1") %>'>
      <table style="width:100%; table-layout: fixed;">
     <asp:CheckBoxList id="CheckBoxList1" CellSpacing="50" TextAlign="Left" 
   runat="server"DataSource='<%#GetChild(Container.DataItem,"ChoicesRelate") 
   %>' DataTextField="CHOICES_CONTENT" DataValueField="CHOICES_NO">
                </asp:CheckBoxList>  
          </table>
     </div>      
    <br />
</ItemTemplate> 
</asp:Repeater>

我参考了下面的答案。 Get radio button list responses with data source asp.net

问题

我无法从复选框列表中获取所有选定的值(只有一个值),而我可以从单选按钮列表中获取所有值.. 我该怎么办? 请帮帮我..

【问题讨论】:

  • 您也可以发布 aspx 代码吗?
  • @MohamedNajiullah - 抱歉,我刚刚编辑了我的帖子。

标签: c# asp.net checkboxlist radiobuttonlist


【解决方案1】:

你可以试试这样的:

using System.Linq
...
foreach (RepeaterItem item in questionRepeater.Items)
{
    // Checking the item is a data item
    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
    {
        var rdbList = item.FindControl("CheckBoxList1") as CheckBoxList;
        // Get the selected value
        if (rdbList != null)
        {
            //get selected items' values 
            List<string> selectedItems = rdbList.Items.Cast<ListItem>()
                .Where(li => li.Selected)
                .Select(li => li.Value)
                .ToList();

            //add to your list of strings
            retList.AddRange(selectedItems);
        }
    }
}

或者,如果你不想使用 Linq,

if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
    var rdbList = item.FindControl("CheckBoxList1") as CheckBoxList;
    // Get the selected value
    if (rdbList != null)
    {
        foreach (ListItem li in rdbList.Items)
        {
            if (li.Selected)
                retList.Add(li.Value);
        }
    }
}

【讨论】:

  • 谢谢!!此解决方案仅适用于复选框?我应该保持从单选按钮列表中获取值的方式吗?
  • @G.G.Sato 由于RadioButtonList 还包含ListItems 的集合,因此您可以以相同的方式遍历它
  • @G.G.Sato ...并且,请,这个答案帮助您并解决了您的问题,将其标记为解决方案;)
  • 哇!!这完全符合我的要求!!谢谢尼诺!ありがとうございます。
猜你喜欢
  • 2018-02-03
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多