C# 获取ListView中所有RadioButtonList的选定值

C# 获取ListView中所有RadioButtonList的选定值,c#,asp.net,listview,radiobuttonlist,ascx,C#,Asp.net,Listview,Radiobuttonlist,Ascx,我有一个列表视图来创建几个RadioButtonList,然后我想在单击按钮后从每个RadioButtonList中获取所选的值 <asp:ListView ID="lvForm" runat="server"> <ItemTemplate> <li> <asp:Label runat="server" Text='<%# Eval("Texto") %>' /&

我有一个列表视图来创建几个RadioButtonList,然后我想在单击按钮后从每个RadioButtonList中获取所选的值

    <asp:ListView ID="lvForm" runat="server">
        <ItemTemplate>
            <li>
                <asp:Label runat="server" Text='<%# Eval("Texto") %>' />
                <br />
                <asp:RadioButtonList runat="server">
                    <asp:ListItem Text="<%$ Resources:liSim.Text %>" Value="<%$ Resources:liSim.Text %>" />
                    <asp:ListItem Text="<%$ Resources:liNao.Text %>" Value="<%$ Resources:liNao.Text %>" />
                </asp:RadioButtonList>
            </li>
        </ItemTemplate>
        <ItemSeparatorTemplate />
        <LayoutTemplate>
            <ol style="list-style-type: upper-alpha;">
                <li id="itemPlaceholder" runat="server" />
            </ol>
        </LayoutTemplate>
    </asp:ListView>

<asp:Button runat="server" ID="btnSeguinte" Text="<%$ Resources:btnSeguinte.Text %>" OnClick="btnSeguinte_Click" />


  • 最好的解决方案是在每个RadioButtonList上执行OnSelectedIndexChanged,并在每次更改后继续保存。但这种变通方法迫使每次更改时都转到服务器端


    如何仅在单击按钮后收集所有选定值?

    您应该能够在ListView中简单地迭代每个项目。首先,在ListView中命名RadioButton列表。这将使它更容易找到

    <asp:RadioButtonList ID="rbList" runat="server">
        <asp:ListItem Text="<%$ Resources:liSim.Text %>" Value="<%$ Resources:liSim.Text %>" />
        <asp:ListItem Text="<%$ Resources:liNao.Text %>" Value="<%$ Resources:liNao.Text %>" />
    </asp:RadioButtonList>  
    
    
    
    然后循环每个项目。在每个项目中查找RadioButton列表。获取刚刚找到的RadioButton列表的SelectedValue,并根据需要使用它

    protected void btnSeguinte_Click(object sender, EventArgs e)
    {
        List<string> selectedValues = new List<string>();
        foreach(ListViewItem item in lvForm.Items)
        {
            RadioButtonList rb = (RadioButtonList)item.FindControl("rbList");
    
            // if none are selected, it returns an empty string
            if(rb.SelectedValue.length > 0)
            {
                selectedValues.Add(rb.SelectedValue);
            }
        }
    
        // do something with your selected values
    
    }
    
    protectedvoidbtnseguinte\u单击(对象发送者,事件参数e)
    {
    List selectedValues=新建列表();
    foreach(lvForm.Items中的ListViewItem项)
    {
    RadioButtonList rb=(RadioButtonList)项。FindControl(“rbList”);
    //如果未选择任何选项,则返回空字符串
    如果(rb.SelectedValue.length>0)
    {
    selectedValues.Add(rb.SelectedValue);
    }
    }
    //使用选定的值执行某些操作
    }
    
    javascript是你最好的朋友,你点击的这个按钮是什么?