envyM6
envyM6

Reputation: 1237

Foreach RadioButtons in ASP.NET Webforms

I'm have ASP MultiView for an application form. Inside one of the view I have few radio button as follows--

<div class="row">
  <asp:CustomValidator ID="Group1Validator" GroupName="Group1" runat="server"
   ValidationGroup="Group1" Display="Dynamic"></asp:CustomValidator>
  <asp:RadioButton ID="Group1Yes" runat="server" GroupName="Group1" Text="Yes"/>
  <asp:RadioButton ID="Group1No" runat="server" GroupName="Group1" Text="No" />
</div>

Now on the code behind I want to iterate through all the RadioButtons in that page and pull the values to be saved in the DB. I have tried the solution HERE both Loop and LINQ version, but the problem is my RadioButton controls are not directly in the page. Let me elaborate-

First, I have multi view in a page-

enter image description here

Multi view contains views-

enter image description here

And finally inside views I have the RadioButton controls-

enter image description here

Now my goal is the fetch the values of all the RadioButton that are selected which is laid out using the div above.

Any help and guidance will be much appreciated! Thanks!

Upvotes: 0

Views: 1107

Answers (2)

VDWWD
VDWWD

Reputation: 35514

Why not use a RadioButtonList

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
    <asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:RadioButtonList>

And then you can get the value in code behind

string value = RadioButtonList1.SelectedValue;

You can also add items to the RadioButtonList in code behind

RadioButtonList1.Items.Insert(0, new ListItem("Yes", "1", true));
RadioButtonList1.Items.Insert(1, new ListItem("No", "0", true));

Upvotes: 1

hubfreak
hubfreak

Reputation: 11

You must put your RadioButtons as childs of RadioButtonList, then iterate in foreach loop over RadioButtonList items.

Upvotes: 1

Related Questions