Asp.net - Add blank item at top of dropdownlist

后端 未结 10 1802
甜味超标
甜味超标 2020-11-30 20:03

Why is the dropdown not showing my blank item first? Here is what I have

drpList.Items.Add(New ListItem(\"\", \"\"))

With drpList
    .DataSource = myContro         


        
相关标签:
10条回答
  • 2020-11-30 20:27

    Like "Whisk" Said, the trick is in "AppendDataBoundItems" property

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.AppendDataBoundItems = true;
            DropDownList1.Items.Insert(0, new ListItem(String.Empty, String.Empty));
            DropDownList1.SelectedIndex = 0;
        }
    }
    

    Thanks "Whisk"

    0 讨论(0)
  • 2020-11-30 20:37

    simple

    at last

    ddlProducer.Items.Insert(0, "");
    
    0 讨论(0)
  • 2020-11-30 20:39

    You can use AppendDataBoundItems=true to easily add:

    <asp:DropDownList ID="drpList" AppendDataBoundItems="true" runat="server"><br/>
        <asp:ListItem Text="" Value="" /><br/>
    </asp:DropDownList>
    
    0 讨论(0)
  • 2020-11-30 20:39

    ddlCategory.DataSource = ds;
    ddlCategory.DataTextField = "CatName";
    ddlCategory.DataValueField = "CatID";

    Cách 1:

    ddlCategory.Items.Add(new ListItem("--please select--", "-1"));
    ddlCategory.AppendDataBoundItems = true;
    ddlCategory.SelectedIndex = -1;

    ddlCategory.DataBind();

    Cách 2:

    ddlCategory.Items.Insert(0, new ListItem("-- please select --", "0"));

    (Tested OK)

    0 讨论(0)
提交回复
热议问题