Search code examples
c#htmlasp.netrepeaterradiobuttonlist

Set id for multiple radio buttons list generated by a repeater


Is there a way to set the ID for multiple radiobuttonlists generated via repeater. ie. ID= rep1 rep2 rep3 rep4 I would like to be able to place my data in multiple columns across a table.

Heres is my code so far:

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %></h2>
<h3>Your application description page.</h3>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
    <ItemTemplate>
        <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Text") %>' />
         <asp:RadioButtonList ID="ddlAnswer" runat="server" AutoPostBack="True" RepeatDirection="Horizontal">
             <asp:ListItem Text="Yes" Value="True" />
             <asp:ListItem Text="No" Value="False" />
          </asp:RadioButtonList>
        <asp:Panel ID="Panel1" runat="server">
            <asp:Label ID="Label1" runat="server" Text="Provide more details:"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </asp:Panel>
        </p>
     </ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [Text] FROM [Questions]"></asp:SqlDataSource>

Behind:

 protected void Page_Load(object sender, EventArgs e)
    {

        foreach (RepeaterItem item in Repeater1.Items)
        {
            RadioButtonList ddlAnswer = item.FindControl("ddlAnswer") as RadioButtonList;
            System.Web.UI.WebControls.Panel Panel1 = item.FindControl("Panel1") as System.Web.UI.WebControls.Panel;

            if (ddlAnswer.SelectedValue == "False")
            {
                Panel1.Visible = true;
            }

            else
            {
                Panel1.Visible = false;
            }
        }

    }

Solution

  • You can just use ClientIdMode="Predictable" attribute in the aspx mark-up to assign different ids to different dropdownlists like this:

    <asp:RadioButtonList ClientIDMode="Predictable" ID="ddlAnswer" runat="server" AutoPostBack="True" RepeatDirection="Horizontal"> ...