How to set dropdown firsl Item -Select- and rest of items from table column?
I am using a dropdown menu to display the Location field of a table. I want to set the first dropdowm item to be "-Select Location-". I cannot set the first record of the tables to be "Select" because the table is being processed in xml format. And the table file is generated dynamically. I use tokenistics like
ddlLocationName.Dispose();
ddlLocationName.AppendDataBoundItems = true;
ddlLocationName.Items.Add("Select Location");
ddlLocationName.DataSource = _section.GetLocations();
ddlLocationName.DataBind();
ddlLocationName.AppendDataBoundItems = false;
but the data is re-bound. What will be the solution to this problem? Thaks in advance.
+1
source to share
2 answers
Once you have your data binding, call ddlLocationName.Items.Insert (0, "Select Location");
Example:
ddlLocationName.Items.Clear();
ddlLocationName.DataSource = _section.GetLocations();
ddlLocationName.DataBind();
ddlLocationName.Items.Insert(0, "Select Location"); // Adds the item in the first position
+1
source to share
Accessing items as ListItems:
ListItem li = new ListItem("Select Location","-1");
ddlLocationName.Items.Add(li);
After you have linked your other data, use:
ddlLocationName.SelectedValue = "-1";
Alternatively, you can add values ββto your table in the same way as the first ListItem.
0
source to share