设置转发器生成的多个单选按钮列表的ID

时间:2015-12-04 15:13:56

标签: c# html asp.net repeater radiobuttonlist

有没有办法为通过转发器生成的多个radiobuttonlists设置ID。 即。 ID = rep1 rep2 rep3 rep4 我希望能够将数据放在表格的多个列中。

到目前为止,Heres是我的代码:

<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>

背后:

 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;
            }
        }

    }

1 个答案:

答案 0 :(得分:1)

您可以在aspx标记中使用ClientIdMode="Predictable"属性为不同的下拉列表指定不同的ID,如下所示:

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