How to add an item to a drop down list in ASP.NET?

后端 未结 3 1349
孤城傲影
孤城傲影 2021-02-01 02:18

I want to add the "Add new" at a specific index, but I am not sure of the syntax. I have the following code:

protected void Page_Load(object sender, Eve         


        
相关标签:
3条回答
  • 2021-02-01 03:18

    Which specific index? If you want 'Add New' to be first on the dropdownlist you can add it though the code like this:

    <asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server">
         <asp:ListItem Text="Add New" Value="0" />
    </asp:DropDownList>
    

    If you want to add it at a different index, maybe the last then try:

    ListItem lst = new ListItem ( "Add New" , "0" );
    
    DropDownList1.Items.Insert( DropDownList1.Items.Count-1 ,lst);
    
    0 讨论(0)
  • 2021-02-01 03:18

    Try following code;

    DropDownList1.Items.Add(new ListItem(txt_box1.Text));
    
    0 讨论(0)
  • 2021-02-01 03:22

    Try this, it will insert the list item at index 0;

    DropDownList1.Items.Insert(0, new ListItem("Add New", ""));
    
    0 讨论(0)
提交回复
热议问题