1

I have a Repeater like this

    <asp:Repeater runat="server" DataSourceID="HeaderFooterSqlDataSource">
        <HeaderTemplate>
            <table border="0" width="100%">
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
            <input runat="server" id="SelectRadio" type="radio" 
 name="HeaderFooter" onclick='SelectAndSetHeaderFooter(this);" %>' />
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>

Now here when this Repeater is rendered the name attribute of my input radio "SelectRadio" gets auto generated but name attribute for all the radiobuttons in my repeater should be same so that they can work like a group & get checked / unchecked automatically according to other elements in that group, So how can I overcome this situation ??

Edit

I got the solution my self, Actually I have defined my input radio control as runat="server" because I thought otherwise Eval() binding wouldn't work but I was wrong Eval() binding does work without runat="server" , So when I remove that attribute name doesn't generated automatically and everything is fine now, But thanx to all for sparing time for my question.

yogi
  • 19,175
  • 13
  • 62
  • 92

4 Answers4

1

Instead of hacking this you should use the built-in RaidoButtonList control.

<asp:RadioButtonList id="RadioButtonList1" runat="server">
    <asp:ListItem>Item 1</asp:ListItem>
    <asp:ListItem>Item 2</asp:ListItem>
    <asp:ListItem>Item 3</asp:ListItem>
    <asp:ListItem>Item 4</asp:ListItem>
    <asp:ListItem>Item 5</asp:ListItem>
    <asp:ListItem>Item 6</asp:ListItem>
</asp:RadioButtonList>
Candide
  • 30,469
  • 8
  • 53
  • 60
  • Actually I don't have to show just text beside a radio button, I have more columns in that Repeater which I haven't posted here, In Short Repeater is must. – yogi Jul 03 '12 at 13:44
1

You want to look into the asp:RadioButton control (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobutton.aspx).

In particular the GroupName property on this control can be used to "specify a grouping of radio buttons to create a mutually exclusive set of controls"

So roughly speaking:

<asp:RadioButton runat="server" id="SelectRadio"
 GroupName="HeaderFooter" %>' />

Edit: It seems that in this particular situation GroupName doesn't do what it is designed for. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobutton.groupname.aspx discusses it in the community content at the end but it boils down to the fact that it uses its current NamingContainer still as part of its name rather than just using the groupname you have given it. Thanks to the mysterious user1429080 for bringing that to my attention in comments.

Chris
  • 27,210
  • 6
  • 71
  • 92
  • 1
    It won't work in a repeater. The repeater will generate a new name for each radiobutton. – user1429080 Jul 03 '12 at 16:19
  • If the GroupName is selected it should use the same name for all of them. Have you confirmed the behaviour you are describing? It looks like you might potentially get in trouble if they are not all children of the same naming container but in the above example it should work because the GroupName is examined in the internal `UniqueGroupName` property of Radiobutton which is where the input name is generated. – Chris Jul 03 '12 at 16:25
  • 1
    The repeater's item template is a naming container. I tested it and it does in fact generate a new name for each radio button. I think it's wrong that it does that, but it's what it does... – user1429080 Jul 03 '12 at 16:54
  • Oh, of course. I was thinking that the repeater would be a naming template and forgot the itemtemplate would be. Stupid me. I am, I have to admit, very surprised that it uses the naming container name at all since it does defeat the point of it but you are right that this would not work in this instance. I wonder if this is considered a known bug.... – Chris Jul 03 '12 at 16:56
0

The reason your radio buttons are not acting as a group is because you have runat="server" which will cause the name and id attributes to be prepended with parents' ids. The following will cause the radio buttons to behave as a cohesive group (note the lack of runat="server"):

ascx code:

<asp:Repeater ID="uxRadioSelections" runat="server">
        <HeaderTemplate>
          <table border="0" width="300px">
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
              <td>
                <input type="radio" value='<%# Eval("Name") %>' name="HeaderFooter" onclick="alert(this.value);"><%# Eval("Name") %></input>
              </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
          </table>
        </FooterTemplate>
    </asp:Repeater>

code behind:

protected void Page_Load(object sender, EventArgs e)
{
   string[] names = new string[] {"Alvin", "Simon", "Theodore"};
   uxRadioSelections.DataSource = names.Select(x => new { Name = x });
   uxRadioSelections.DataBind();
}

However, the following will NOT perform as a cohesive group:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    Test
    <asp:Repeater ID="uxRadioSelections" runat="server">
        <HeaderTemplate>
          <table border="0" width="300px">
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
              <td>
                  <asp:RadioButton Text='<%#Eval("Name") %>' runat="server" />
              </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
          </table>
        </FooterTemplate>
    </asp:Repeater>
</asp:Content>

Due to the naming conventions you alluded to, these individual radio buttons will not find each other. ASP.NET uses RadioButtonLists to overcome this:

ascx code:

<asp:RadioButtonList ID="uxRadioButtons" runat="server">
</asp:RadioButtonList>

code behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        string[] names = new string[] {"Alvin", "Simon", "Theodore"};
        uxRadioButtons.DataTextField = "Name";
        uxRadioButtons.DataValueField = "Name";
        uxRadioButtons.DataSource = names.Select(x => new { Name = x });
        uxRadioButtons.DataBind();
    }

This does not give you as granular control, but will provide easy server-side access to the selections. If all you need is javascript-related functionality and increased formatting flexibility, the first format may (which is nearly identical to what you provided) is your best bet. Good luck!

Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
  • I guess you did got the point here, The `NAME` attribute of my input radio control gets auto generated because it is in a databinding control(Repeater), because of which all instance of that radio control gets unique name so due to that uniqueness they don't act like a group and I can check multiple radio button at a time which is wrong because I want one radio button to checked at a time which is a normal behavior. – yogi Jul 03 '12 at 13:50
  • 2
    That is not the behavior you should be experiencing. The "name"/"id" only gets generated for runat="server" based controls, client controls (such as ) do not receive the same treatment and thus will remain grouped as you have coded. If you remove runat="server" in your example, your code will work as a group. If you need server-side handling of your selections but still need the flexibilty, might I suggest either jQuery ajax calls or old-school __doPostBacks? – Jaime Torres Jul 03 '12 at 13:54
  • Updated answer to clarify for future users. Glad you "got the solution yourself." – Jaime Torres Jul 03 '12 at 14:19
  • @JTorres: Just to correct your comment "ASP.NET uses RadioButtonLists to overcome this": this isn't strictly true. RadioButtonLists are one way of doing it but the RadioButtons are quite capable of doing it themselves just using a groupname. I would imagine the differences are that using GroupName the radio buttons don't have to be next to each other whereas I would expect RadioButtonList to have nicer ways of finding the selected radiobutton without having to query them all... – Chris Jul 03 '12 at 15:36
  • @Chris Perhaps I could have been more clear, but the "this" I was referring to was generating a radiobutton group based on a single datasource since that seemed to be the requirement. As far as I know, a RadioButtonList is the only way to go about doing so with OOTB controls. – Jaime Torres Jul 03 '12 at 15:39
  • @JTorres: I don't see why you couldn't do it with at RadioButton control. You just put a radioButton control in as in your second name but put `GroupName="LinkedButtons"` and all the controls will have that group name and thus have the mutually exclusive property that is needed. – Chris Jul 03 '12 at 15:43
  • Comments on my answer have explained why this in fact doesn't work despite the fact the documentation suggests it probably should. :( – Chris Jul 03 '12 at 16:57
0

Another option is to set the ClientIDMode as 'static' which will set its value to that of the ID specified. Since it is a repeater control, all the items generated will thus have the same generated ID. So you simply set:

<asp:RadioButton runat="server" id="SelectRadio" GroupName="HeaderFooter" ClientIDMode="static">' />

This means that all generated controls will have the same ID specified i.e. "SelectRadio". This may or may not be problematic and you should be careful that this is exactly what you want.

Just a note, ClientIDMode is a feature of ASP.Net 4.0. More info here: ClientIDMode in ASP.NET 4.0 and here: MSDN. I hope this helps.

Update 1: You can get more insights from this question asked before on StackOverflow. (It's jQuery though).

Community
  • 1
  • 1
Wilbur Omae
  • 184
  • 2
  • 11