我试图使用这样的 for 循环创建下拉列表:
private void Page_Load(object sender, System.EventArgs e)
{
//build placeholder
for(int x = 0; x <= 10; x++) {
DropDownList dp = new DropDownList();
dp.ID = "carrirerDp " + x.ToString();
dp.DataSource = PhoneCarrierSqlDataSource;
dp.DataTextField = "carrier_name";
dp.DataValueField = "crr_id";
DropDownPlaceHolder.Controls.Add(dp);
DropDownPlaceHolder.Controls.Add(new LiteralControl("<br>"));
}
}
下拉菜单已创建,但它们是空的。执行我用 asp 标记创建的那个
<asp:DropDownList ID="PhoneCarrierDropDownList" runat="server"
DataSourceID="PhoneCarrierSqlDataSource" DataTextField="carrier_name"
DataValueField="crr_id">
</asp:DropDownList>
<asp:SqlDataSource ID="PhoneCarrierSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT *
FROM Table">
</asp:SqlDataSource>
这是占位符:
<asp:PlaceHolder id="Area1" runat="server"></asp:PlaceHolder>