将 DropDownLists 和 Button 控件添加到中继器中的 PlaceHolder 控件:
<asp:Repeater ID="Repeater1" runat="server" EnableViewState="true"
onitemcommand="Repeater1_ItemCommand" onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="one"></asp:ListItem>
<asp:ListItem Text="two"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Text="three"></asp:ListItem>
<asp:ListItem Text="four"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" UseSubmitBehavior="false" Text="Button" CommandName="btn" />
</asp:PlaceHolder>
</ItemTemplate>
</asp:Repeater>
在中继器的 ItemCommand 事件中,通过单击按钮检查 CommandName,然后创建并将动态下拉列表添加到占位符:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "btn")
{
DropDownList ddl = new DropDownList();
ddl.ID = "DropDownList1";
ddl.DataSource = new string[] { "one", "two" };
ddl.DataBind();
// your second dropdown would be created here in the same way
pl.Controls.Add(ddl);
}
}
连接 SelectedIndexChanged 事件还有一些工作要做,但这应该可以帮助您入门。