only one radiobutton selection in repeater
Asked Answered
C

2

4

I want to select only one radio button but I cant do this. Here is my code:

<asp:Repeater ID="Repeater_secenekler" runat="server" DataSource='<%#Eval("Secenekler")%>'>
   <HeaderTemplate>
       <table>
   </HeaderTemplate>
   <ItemTemplate>
       <tr>
           <td style="margin-left: 20px;">
               <asp:CheckBox ID="CheckBox" runat="server" Text='<%#Eval("OptionName")%>' Visible='<%# Eval("TypeId").ToString() == "1" %>' />
               <asp:RadioButton ID="RadioButton" runat="server" Text='<%#Eval("OptionName")%>' Visible='<%# Eval("TypeId").ToString() == "2" %>'
                    GroupName="RadioButtonGrup" />
           </td>
       </tr>
   </ItemTemplate>
   <FooterTemplate>
       </table>
   </FooterTemplate>
</asp:Repeater>

I can select all radio button with same groupname but I dont want this. I want only one selection.

How can I do this. Thanks.

Clue answered 15/6, 2012 at 13:34 Comment(0)
E
14

This is a well known bug with the ASP.NET Repeater using RadioButtons:

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q316495

There is a fix available here:

http://www.ifinity.com.au/Blog/EntryId/87/Simple-fix-for-Radio-Button-controls-in-an-ASP-NET-Repeater-using-jQuery

jQuery("[name$='$optValue']").attr("name",jQuery("[name$='$optValue']").attr("name"));

jQuery("[name$='$optValue]").click(function (){ 
                //set name for all to name of clicked 
                jQuery("[name$='$optValue]").attr("name", this.attr("name")); 
            });
Enos answered 15/6, 2012 at 13:40 Comment(1)
great shout... have a donut ;-)Crosscut
D
9

Just a quick fix of the above answer so you don't get any errors when the script is executed.

    $(function () {
    $('[name$="$YourGroupName"]').attr("name", $('[name$="$YourGroupName"]').attr("name"));

    $('[name$="$YourGroupName"]').click(function () {
        //set name for all to name of clicked 
        $('[name$="$YourGroupName"]').attr("name", $(this).attr("name"));
    });
});
Dealing answered 13/6, 2013 at 8:20 Comment(3)
In fact the previous solution didn't work for me until you (@xfan) made this fix. Thanks!!Longitudinal
Thank you! This answer cleared my issues with the fix right up.Disentwine
Why not just set the name attribute of all the radio buttons to the same value and leave it at that?Arrogance

© 2022 - 2024 — McMap. All rights reserved.