之前把DropDownList放在Repeater里面,结果显示一堆下拉列表。
代码:
(前台)
<asp:DropDownList ID="brand" runat="server" OnLoad="brand_Load">
</asp:DropDownList>
<asp:Repeater ID="rp1" runat="server" >
<ItemTemplate>
<asp:Label runat="server" ID="brands" Text=‘<%# Eval("name") %>‘ Visible="false"/>//只是为把Label里面的Text在后台赋值给DropDownList的ListItem
</ItemTemplate>
</asp:Repeater>
(后台)
protected void brand_Load(object sender, EventArgs e)
{
foreach (RepeaterItem item in rp1.Items)//遍历RepeaterItem
{
Label lbId1 = item.FindControl("brands") as Label;
brand.Items.Add(new ListItem(lbId1.Text,lbId1.Text));//两个参数,一个text,一个value
}
}
效果: