簡體   English   中英

如何從單選按鈕列表和復選框列表中獲取選定的值?

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

我正在嘗試從C.asp.net的單選按鈕列表和復選框列表中獲取選定的值。 這是我的代碼。

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>

我在下面提到了答案。 使用數據源asp.net獲取單選按鈕列表響應

問題

我不能從復選框列表中獲得所有選定的值(僅一個值),而我卻可以從單選按鈕列表中獲得所有的值。我該怎么辦? 請幫我..

您可以嘗試這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM