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

ArshadRank #2,492 · 564 reputationPosted Jul 11, 2010, 8:09 PM EDT
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.
rasagna ramireddy10 reputationPosted Jul 9, 2010, 10:16 PM EDT
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
ArshadRank #2,492 · 564 reputationPosted Jul 9, 2010, 10:55 PM EDT
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
Ellen Hu55 reputationPosted Jul 10, 2010, 7:23 AM EDT
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
Ellen Hu55 reputationPosted Jul 12, 2010, 5:49 AM EDT
Really appreciate your time.
ellen