获取 ListView 中所有 RadioButtonLists 的选定值
Get selected values of all RadioButtonLists inside a ListView
我有一个 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 并在每次更改后继续保存。但是这种解决方法会强制在每次更改时转到服务器端。
如何仅在单击按钮后收集所有选定值?
您应该能够简单地迭代 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
}
我有一个 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 并在每次更改后继续保存。但是这种解决方法会强制在每次更改时转到服务器端。
如何仅在单击按钮后收集所有选定值?
您应该能够简单地迭代 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
}