【问题标题】:Get selected values of all RadioButtonLists inside a ListView获取 ListView 内所有 RadioButtonLists 的选定值
【发布时间】:2015-07-16 22:38:20
【问题描述】:

我有一个 ListView 来创建几个 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 并在每次更改后继续保存。但是这种解决方法会在每次更改时强制转到服务器端。

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

【问题讨论】:

  • javascript 是你最好的朋友
  • 您点击的这个按钮在哪里,是什么?

标签: c# asp.net listview radiobuttonlist ascx


【解决方案1】:

您应该能够简单地迭代 ListView 中的每个项目。首先,在 ListView 中命名 RadioButtonList。这样更容易找到。

<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>  

然后循环每个项目。在每个项目中找到 RadioButtonList。获取您刚刚找到的 RadioButtonList 的 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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多