简体   繁体   English

如何从单选按钮列表和复选框列表中获取选定的值?

[英]How can I get selected values from radio button list and check box list?

I'm trying to get selected values from radio button list and check box list at asp.net, C#. 我正在尝试从C.asp.net的单选按钮列表和复选框列表中获取选定的值。 Here's my code. 这是我的代码。

aspx.cs 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 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>

I refered below Answser. 我在下面提到了答案。 Get radio button list responses with data source asp.net 使用数据源asp.net获取单选按钮列表响应

problem 问题

I can't get all selected values(just one value) from check box list, while I can get all from radio button list.. What should I do? 我不能从复选框列表中获得所有选定的值(仅一个值),而我却可以从单选按钮列表中获得所有的值。我该怎么办? Please help me.. 请帮我..

you can try something like this: 您可以尝试这样的事情:

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);
        }
    }
}

or, in case you don't want to use Linq, 或者,如果您不想使用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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取列表框中所选项目的索引值? - How do I get the index values of the selected items in a list box? 多个单选按钮列表,获取控制器中的选定值 - Multiple radio button list, get selected values in controller 如何从WPF列表框中获取选定列表项中的选定单选按钮 - How to get selected radio button in selected list item from wpf listbox 如何获取嵌套在数据列表中的单选按钮列表的文本值 - How to Get Text Values of a Radio button list that is nested in a data list 如何使用jQuery获取或设置单选按钮列表的选定索引? - How to get or set Selected index of the Radio Button List using jquery? 在gridview上检查所有单选列表单选按钮是否被选中 - check all radio list radio button has been selected or not on gridview 获取选定的单选按钮不在ASP .NET的列表中 - Get selected radio button not in a list in ASP .NET 从带有复选框的列表框中获取选定的项目 - Get selected items from a list box with check boxes 选择单选按钮列表项后如何从gridview获取单元格值? - How to get cell value from gridview when radio button list item is selected? 如何在中继器中找出单选按钮列表的选定值 - How can I find out the selected value of Radio Button List in Repeater
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM