I have a ListView to create several RadioButtonList, then I want to get the selected values from each RadioButtonList after a button click.
我有一个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" />
The best solution was to do OnSelectedIndexChanged on each RadioButtonList and keep saving after each change. But this workaround forces to go server-side at each change.
最好的解决方案是在每个RadioButtonList上执行OnSelectedIndexChanged并在每次更改后保持保存。但是这种解决方法迫使每次更改都要服务器端。
How can I collect all selected values only after a button click?
如何在单击按钮后收集所有选定的值?
1 个解决方案
#1
You should be able to simply iterate each item in the ListView. First, name your RadioButtonList in your ListView. It will make it easier to find.
您应该能够简单地迭代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>
Then loop each item. Find the RadioButtonList in each item. Get the SelectedValue of the RadioButtonList you just found and use it however you'd like.
然后循环每个项目。在每个项目中找到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
}
#1
You should be able to simply iterate each item in the ListView. First, name your RadioButtonList in your ListView. It will make it easier to find.
您应该能够简单地迭代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>
Then loop each item. Find the RadioButtonList in each item. Get the SelectedValue of the RadioButtonList you just found and use it however you'd like.
然后循环每个项目。在每个项目中找到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
}