views:

364

answers:

2

Hi, I m using a dropdown to display "Location" field of a table. I want to set first item of dropdowm as "-Select Location-". I can't set tables first record as "Select" because table is stroed in xml format. And table file is generated dynamicaly. I am currentaly using as

    ddlLocationName.Dispose();
    ddlLocationName.AppendDataBoundItems = true;
    ddlLocationName.Items.Add("Select Location");        
    ddlLocationName.DataSource = _section.GetLocations();
    ddlLocationName.DataBind();
    ddlLocationName.AppendDataBoundItems = false;

but data is binded repeatedly. What will be the solution for this problem? Thaks in advance.

A: 

Access the items in the form of listitems as :

ListItem li = new ListItem("Select Location","-1");
ddlLocationName.Items.Add(li);
After you have bound ur other data,use,
ddlLocationName.SelectedValue = "-1";

Also, you can add the values of ur table in similar way to list item first .

Samiksha
+1  A: 

After you have databound, then 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
Tom Jelen