C# – Adding Item to DataBound Drop Down List

asp.netc++data-bindingdrop-down-menulist

Yes, I have read most of the topics here, but I can't find an answer that works.

I have Three drop-down lists. The first is databound to grab distinct experiment names. The user selects, page posts back, and the second drop-down menu displays distinct time points. This is where I need help. I need to add an item to THAT drop-down list whose ID, DataTextField, DataValueField are all TimePt.

Seems simple, but I can't get it to work.

protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                TimePt.DataSource = TimePTDD;
                TimePt.DataValueField = "TimePt";
                TimePt.DataTextField = "TimePt";
                TimePt.DataBind();
                TimePt.Items.Insert(0, new ListItem("--Select---", "0"));
                TimePt.SelectedIndex = 0;
            }
        }

I'm missing sometthing.

Best Solution

Set AppendDataBoundItems="true" on your dropdown list and it should work.

Here's a similar question: How to add Item to SqlDataSource databound list

And another one about potential duplicates using this method and a workaround for it: Dropdownlist AppendDataboundItems (first item to be blank)