tags:

views:

24

answers:

3
 Hi, 

I try to add a new item to the drodownlist.I want the added item to be place at first position of a dropdownlist.How can I do that

   I want this to be a first option
   ListItem itemspecial = new ListItem();
   itemspecial.Value = "-1";
   itemspecial.Text = "Please select specialization";
   ddlSpecialization.Items.Add(itemspecial);

    ddlSpecialization.DataTextField = "Description";
    ddlSpecialization.DataValueField = "Id";
    ddlSpecialization.DataSource = objspecial.GetAll();
    ddlSpecialization.DataBind();
A: 

You can do this using the Insert function of the DropDownList:

ListItem itemspecial = new ListItem();
itemspecial.Value = "-1";
itemspecial.Text = "Please select specialization";
ddlSpecialization.Items.Insert(0, itemspecial);

ddlSpecialization.DataTextField = "Description";
ddlSpecialization.DataValueField = "Id";
ddlSpecialization.DataSource = objspecial.GetAll();
ddlSpecialization.DataBind();
Druid
A: 

use -- ddlSpecialization.Items.insertAt(itemspecial,0);<-Specify the first index,

pjb
A: 

Databind first before you insert...

ddlSpecialization.DataTextField = "Description";
ddlSpecialization.DataValueField = "Id";
ddlSpecialization.DataSource = objspecial.GetAll();
ddlSpecialization.DataBind();

ddlSpecialization.Items.Insert(0, new ListItem("Please select specialization", "-1"));
hallie