0

我试图使用这样的 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>
4

1 回答 1

4

您还没有数据绑定:

 dp.DataBind();
于 2013-10-03T20:58:43.073 回答