views:

1135

answers:

2

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?

+2  A: 

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.

Bob
works like a charm !! THANKS :)
Murtaza RC
Awesome, I was worried I had a syntax error, coding without Intellisense is always more difficult.
Bob
A: 

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.

Mark Bertenshaw