0

I have the online exam system that is using input with radio type my problem is I can't get selected input radio

<form runat="server"">
    <asp:Repeater ID="rptCustomers" runat="server">


<ItemTemplate>
 <form>
<div class="question">
    <div class="container">
        <p class="title">

        </p>
        <asp:Label CssClass="title" runat="server" Text='<%# Eval("Title") %>'/>

        <ul>
            <li>
                <input type="radio" name="radio" value="1">
                <label><%# Eval("r1") %></label>
            </li>
            <li>
                <input type="radio" name="a" value="1">
                <label><%# Eval("r2") %></label>
            </li>
            <li>
                <input type="radio" name="a" value="1">
                <label ><%# Eval("r3") %></label>
          </li>
          <li>
            <input  type="radio" name="a" value="1">
            <label ><%# Eval("r4") %></label>
      </li>
        </ul>
    </div>       
</div> 
     </form>
    </ItemTemplate>

when I add runat=server to inputs I can't get them again in code behind anyway to get selected radio input? I am using c#

KnightM
  • 25
  • 1
  • 5
  • Either use `runat="server"` and `GroupName="a"` and assign unique names `Name="a1", Name="a2" etc.` for each radio button, or, if you go with plain HTML inputs, give the radio buttons the same name but set unique values for each. – Oguz Ozgul Mar 23 '20 at 21:27
  • They are inside a `` ItemTemplate so you have to use `.FindControl` of that repeater to find a reference to the radios. Also, you are nesting `
    ` tags which is invalid
    – zgood Mar 23 '20 at 21:32
  • can you please tell how to find it? – KnightM Mar 23 '20 at 22:14
  • and the second problem is if I dont put them into form I cant create questions because the radio going to be changed foreach question – KnightM Mar 23 '20 at 22:16
  • 1
    @KnightM look at this [SO post](https://stackoverflow.com/questions/46794933/how-to-access-specific-controls-in-a-asp-net-repeater), it should help you out. Or this [post's answer](https://forums.asp.net/t/2023007.aspx?I+can+t+access+the+controls+in+the+repeater+from+the+code+behind), specifically the `foreach (RepeaterItem item in rptComments.Items)` and the `Button btncontrol = item.FindControl("btnEditComment") as Button;` bits of code – zgood Mar 24 '20 at 13:26

1 Answers1

2

Here is a solution with radioButtonList:

<div class="row">
        <div class="col-md-4">
            <h2>Getting started</h2>
            <asp:RadioButtonList ID="RadioButtonList1" runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
                <asp:ListItem>item1</asp:ListItem>
                <asp:ListItem>item2</asp:ListItem>
                <asp:ListItem>item3</asp:ListItem>
            </asp:RadioButtonList>
        </div>
    </div>

and then access the value of the selected item :

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            String string1 = RadioButtonList1.SelectedItem.Text;
        }

Do not forget to check "Enable AutoPostBack" so you will notice the changes of switching between the radio buttons.