Adding Item to DataBound Drop Down List

23,833

Solution 1

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)

Solution 2

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            TimePt.DataValueField = "TimePt";
            TimePt.DataTextField = "TimePt";
            var times = TimePTDD.ToList();
            times.Insert(0, new {TimePt="0",TimePt="--Select--"});
            TimePt.DataSource = times;
            TimePt.DataBind();
            //TimePt.SelectedIndex = 0;
        }
    }
Share:
23,833
Admin
Author by

Admin

Updated on December 09, 2020

Comments

  • Admin
    Admin over 3 years

    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.

  • Admin
    Admin almost 15 years
    It won't do what I'd like it to do. I tried that and it works, but whenever I go back and choose the first drop down list, I get duplicate values in the second drop down list (which is where I want the "SELECT" to be. That link also doesn't help me because I try it and it says I don't have a method named "MyList" which is what I named it.
  • Ahmad Mageed
    Ahmad Mageed almost 15 years
    Did you intend to use if (Page.IsPostBack) rather than if (!Page.IsPostBack) in your page load?
  • Admin
    Admin almost 15 years
    Yes, that's what I need it to do when I change the first drop down list, but I need "-select-" in the TimePt drop down list without having duplicate values as a result of rebinding that list because the data from the first down menu changed.
  • Admin
    Admin almost 15 years
    I intend to use IsPostBack because when you select the first drop down list, the page loads and the 2nd drop down list populates based on what's selected from the first drop down list.
  • Ryan Versaw
    Ryan Versaw almost 15 years
    This shouldn't generate duplicate values as AppendDataBoundItems is not set to true.
  • Admin
    Admin almost 15 years
    No, it shouldn't because I turned it off, but if I turn it on, then it will cause duplicate values, but I will get -- Select -- to stay if I use <asp:ListItem text="--Select--"></asp:ListItem>, but I want this item to appear always, regardless of what I choose from the first drop down menu and then the subsequent items be dynamic based on what was selected :).
  • Admin
    Admin almost 15 years
    The thing is here is that some experiments have three time points, while some have one or two.