简体   繁体   中英

Problem with HTML radio button inside repeater

I have a HTML radio button inside a repeater. I'm facing two problems:

  1. When I'm binding Value from code behind (using datatable in code behind) it is throwing error server tag not found.

  2. How to get the selected value in code behind

My code till now is this.

asp:Repeater runat="server" ID="rptr1">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
   <tr> <td>
      <span><input type="radio" runat="server" name="acd" class="radio" id="radio1" value='<%# DataBinder.Eval(Container.DataItem, "valuinfm")%>'/></td><td> <label for="radio"><%# DataBinder.Eval(Container.DataItem, "txtinfm")%></label></span></td>
              <br />
    </tr>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:Repeater>

I used this approach because there was some bug in using ASP.NET radio button with repeater. I don't want to use JavaScript.

Basically I want to redirect to other page by having querystring which is "value" of radio button.

Edited: // I have corrected it to single outer quotes now there is no error (Thanks to Brian).

Values in radio button are different but still I can click both and also how I can detect which radio button is selected:

Why not use the server radio button as in:

<asp:RadioButton id="rad1" runat="server" GroupName="acd" ... 
   Checked='<%# Eval("valuinfm") %>' />

Note the single quotes around the property value too, when inner quotes are double, you need single on the outside.

Well for starters, this is going to generate multiple radio buttons with the same ID in the browser, which is a no-no. It might also be part of your problem, but it would help to see the code which is actually throwing the error during runtime.

The problem is because the name attribute of the generated radio buttons gets a UniqueId appended to it. This is .NET trying to be helpful but in this case it causes problems as the browser groups radio buttons where the name attribute is the same. One possible solution is to create your own radio button web control which doesn't override the name attribute as discussed in this article:

http://www.codeproject.com/KB/webforms/How_group_RButtons.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM