Alex Rivera | Logout

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

Asked 2013-07-04T11:19:35.797
40

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, EventArgs e)
{
        DRPFill();
        if (!IsPostBack)
        {
            DropDownList1.Items.Add("Add New");
        }
}
public void DRPFill()
{
    if (!IsPostBack)
    {
        //Object
        AddMajor objMajor = new AddMajor();

        //Data Table
        DataTable dtMajor = objMajor.find();

        //Data Source
        DropDownList1.DataSource = dtMajor;
        DropDownList1.DataValueField = "MajorID";
        DropDownList1.DataTextField = "MajorName";

        //Data Bind
        DropDownList1.DataBind();
    }
}
Edit
Report

1 Answer

62

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

DropDownList1.Items.Insert(0, new ListItem("Add New", ""));
answered 2013-07-04T11:24:34.457

Your Answer