Murtaza Mandvi
Murtaza Mandvi

Reputation: 10988

HTML Radio button inside ASP.NET repeater C#

I am trying to simply bind a HTML Input radio button inside a repeater :

<asp:Repeater ID="Outlets" runat="server" >
 <ItemTemplate>
    <input type="radio" name="Proposal" value="Test1" id="Radio1" checked="
<%#`GetSelectedValue(DataBinder.Eval(Container.DataItem,"IsDefaultSelection"))%>" />`

    <label for="Test1">
              <%#DataBinder.Eval(Container.DataItem, "Label")%>
    </label>

  </ItemTemplate>
</asp:Repeater>

The GetSelectedValue method simply returns a string "checked" for the radio button to be selected, lets say the repeater had 3 items to loop and when the markup renders and If I inspect the element I see the correct value for the first radio button i.e. checked="checked" but on the UI the 3rd radio button gets selected even though its checked value="false", can someone guide me as to what i am doing wrong?

Upvotes: 2

Views: 5202

Answers (2)

Mark Bertenshaw
Mark Bertenshaw

Reputation: 5689

The CHECKED attribute in HTML has no value associated with it. You should only use this attribute if you want to select it.

You want something like:

<asp:Repeater ID="Outlets" runat="server" >
 <ItemTemplate>
    <input type="radio" name="Proposal" value="Test1" id="Radio1" <%
    if (GetSelectedValue(DataBinder.Eval(Container.DataItem,"IsDefaultSelection"))
        response.write("CHECKED");
%> />
    <label for="Test1">
              <%#DataBinder.Eval(Container.DataItem, "Label")%>
    </label>

  </ItemTemplate>
</asp:Repeater>

Otherwise you could simply use the ASP.NET INPUT controls.

Upvotes: 0

Bob
Bob

Reputation: 99734

Give this a try

<%# (bool)Eval("IsDefaultSelection") ? " checked=\"checked\" " : string.Empty %>

Basically the inclusion on the checked attribute is causing the RadioButton to be checked. You want to pull it out so the entire thing is rendered or the entire thing is absent.

Also keep in mind all your RadioButtons are going to be named Test1 It might be a good idea to use a server side RadioButton.

Upvotes: 4

Related Questions