I have a ListView to create several RadioButtonList, then I want to get the selected values from each RadioButtonList after a button click.
<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.
How can I collect all selected values only after a button click?
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.
<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.
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
}