C# 如何将listitem添加到代码隐藏中生成的dropdownlist?

C# 如何将listitem添加到代码隐藏中生成的dropdownlist?,c#,asp.net,drop-down-menu,C#,Asp.net,Drop Down Menu,我在代码隐藏中动态创建下拉列表。我想添加一个列表项,它是下拉列表的默认选择 int Persons= int.Parse(TextBox_persons.Text) + 1; for (int i = 1; i < Persons; i++) { DropDownList DropDownList_menuchoice = new DropDownList(); DropDownList_menuchoice.DataSource = Menu.GetAllMenus

我在代码隐藏中动态创建下拉列表。我想添加一个列表项,它是下拉列表的默认选择

int Persons= int.Parse(TextBox_persons.Text) + 1;

for (int i = 1; i < Persons; i++)
{
     DropDownList DropDownList_menuchoice = new DropDownList();
     DropDownList_menuchoice.DataSource = Menu.GetAllMenus();
     DropDownList_menuchoice.CssClass = "form-control";
     DropDownList_menuchoice.Items.Add(new ListItem("please select a menu", "-1"));
     DropDownList_menuchoice.DataTextField = "titel";
     DropDownList_menuchoice.DataValueField = "titel";
     DropDownList_menuchoice.DataBind();
     Panel1.Controls.Add(DropDownList_menuchoice);
}
int Persons=int.Parse(TextBox\u Persons.Text)+1;
对于(int i=1;i
为什么这样不行?我一直在网上寻找答案,但是,每个人都在建议
项目。添加
代码,它对我不起作用。它只显示我从数据库检索的数据,而不是我在上面代码中添加的listitem


如果您知道原因,请帮助我。

您需要在
pre_init
事件上重新创建您的下拉列表

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
    //create your dropdown here
}
编辑-1 关于pre_init

protected override void OnPreInit(EventArgs e)
{
    int Persons=0;
    if(Session["Persons"]!=null)
    Persons= int.Parse(Session["Persons"].ToString()) + 1;
    for (int i = 1; i < Persons; i++)
    {
        DropDownList DropDownList_menuchoice = new DropDownList();
        DropDownList_menuchoice.DataTextField = "titel";
        Panel1.Controls.Add(DropDownList_menuchoice);
     }
    base.OnPreInit(e);
}
protectedoverride void OnPreInit(EventArgs e)
{
整数人=0;
如果(会话[“人员”]!=null)
Persons=int.Parse(会话[“Persons”].ToString())+1;
对于(int i=1;i
首先,您需要在调用DataBind方法后添加项。 像这样:

DropDownList DropDownList_menuchoice = new DropDownList();
DropDownList_menuchoice.DataSource = Menu.GetAllMenus();
DropDownList_menuchoice.CssClass = "form-control";
DropDownList_menuchoice.DataTextField = "titel";
DropDownList_menuchoice.DataValueField = "titel";
DropDownList_menuchoice.DataBind();
DropDownList_menuchoice.Items.Add(new ListItem("please select a menu", "-1"));
var data = Menu.GetAllMenus();
data.Insert(0, new Menu { titel = "please select a menu" });
DropDownList_menuchoice.DataSource = data;
...
然后需要使用Insert方法将其添加到索引0(作为第一项):

您还可以先将该项添加到数据中,然后设置DataSource属性。大概是这样的:

DropDownList DropDownList_menuchoice = new DropDownList();
DropDownList_menuchoice.DataSource = Menu.GetAllMenus();
DropDownList_menuchoice.CssClass = "form-control";
DropDownList_menuchoice.DataTextField = "titel";
DropDownList_menuchoice.DataValueField = "titel";
DropDownList_menuchoice.DataBind();
DropDownList_menuchoice.Items.Add(new ListItem("please select a menu", "-1"));
var data = Menu.GetAllMenus();
data.Insert(0, new Menu { titel = "please select a menu" });
DropDownList_menuchoice.DataSource = data;
...

当下拉列表在for循环中动态创建时,我该怎么做?您可以在
会话
pre_init
循环中保留值,并创建动态控件查看我的更新您是否尝试在调用数据绑定下拉列表后添加默认项?这正是我想要的,非常感谢,它现在工作得很好。你太棒了:)