I have a country drop down list, the listitems are from database. At code behind I had added "--Select Country--" as default item by using:
ddlCountry.Item.Add(0, "--Select Country--"); after binding.
Now if I want to set United Stated as 2nd item, and the other countries still will be in alphabetical order. How can achive this task?
Thank you in advance. Any comment will be highly appreciated.
ellen
ArshadPosted Jul 12, 2010, 12:09 AM
ddlCountry.Items.Insert(0, new ListItem("Select"));
ListItem lst;
lst = ddlCountry.Items.FindByText("USA");
ddlCountry.Items.Remove(lst);
ddlCountry.Items.Insert(2, lst);
First we have to remove that item from the lst and then add. it will work.
Regards,
Arshad.
Ellen HuPosted Jul 12, 2010, 9:49 AM
Really appreciate your time.
ellen
Ellen HuPosted Jul 10, 2010, 11:23 AM
Really helps.
But now on this drop down will have 2 USA. How can I keep only 1 ?
Thank you again for your help.
ellen
ArshadPosted Jul 10, 2010, 2:55 AM
Please use the below code, its tested code, it will solve your problem.
ddlCountry.Items.Insert(0, new ListItem("Select"));
ListItem lst;
lst = ddlCountry.Items.FindByText("USA");
ddlCountry.Items.Insert(2, lst);
Regards,
Arshad
rasagna ramireddyPosted Jul 10, 2010, 2:16 AM
I am correct , United States and all are from Database and you want to bind that data to dropdownlist with select as default value!!!!!!
then,
Instead of setting default item in code behind,just set it in source it self so that it will serve your problem as well limits the load on code
see the sample below
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem >--select--asp:ListItem>
asp:DropDownList>div>
hope this helps:)
Thanks
Rasagna