Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi geeks,

Just small verification.

I need to load "Select" in dropdownlist at pageload.

Then later on I add an item after typed in textbox.
C#
DropDownList d2 = (DropDownList)gvrow.Cells[2].FindControl("ddlfabric");
            d2.Items.Insert(0, new ListItem("Select"));
            if (txtfabric.Text != "")
            {   
                if(!d2.Items.Contains(new ListItem(txtfabric.Text)))
                {
                    d2.Items.Insert(1, txtfabric.Text);
                }
            }


The duplicates are removed but not the word "Select". Why Select is repeated ???
Posted
Comments
[no name] 1-Jul-14 7:48am    
It's repeated because you added it again.
Might be due to the below line...

d2.Items.Insert(0, new ListItem("Select"));
Sriram Ramachandran 1-Jul-14 8:04am    
I don't know how it is added again as I used items.insert("select") once.

1 solution

In Page_Load()

C#
if (!Page.IsPostBack)
{
  //Add your "Select" Item here.
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900