I am binding a drop down from the database in the follwing way
-- Select an item --
Bicycles
Matresses
Games
And I am selecting the first item as default.. but if the client is an existing client and have saved this before so the table will say that they chose for example “Matresses”, the problem I am having is binding it and diplaying “Matresses” as selected when they client comes back to this page.. when i do
ddlmyItems.SelectedIndex = ddlmyItems.Items.IndexOf
(ddlmyItems.Items.FindByText("Matresses"));
It is choosing the correct index, but the drop down looks like this
-- Select an item --
Bicycles
-- Select an item -- //this is supposed to be Matresses
Games
I also tried changing the text back to its original name but it would not work
ddlmyItems.SelectedItem.Text = "Matresses";
Any idea why it is not picking up the correct “Text” on the third item?, or how can i do this?
Thank you
==========================================================
private void BindShippingDropDown(TList<StoreItems> storeItemsList, string countryCode)
{
ddlmyItems.Items.Clear();
ListItem liDefault = new ListItem("Select an Item", "0");
ddlmyItems.Items.Add(liDefault);
DataSet ds = storeItemsList.ToDataSet(false);
DataView dv = new DataView(ds.Tables[0]);
dv.RowFilter = "DestinationCountryCode = '" + countryCode + "' or DestinationCountryCode is null";
if (dv.ToTable().Rows.Count > 0)
{
foreach (DataRow dr in dv.ToTable().Rows)
{
string description = dr["Description"].ToString();
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("®");
description = regex.Replace(description, "®");
ListItem li = new ListItem(description, dr["ItemID"].ToString());
ddlmyItem.Items.Add(li);
}
ddlmyItems.SelectedIndex = 0;
}
}
If I had to take a stab at this, I think your duplicate “Select an Item” is being caused by having
AppendDataBoundItems="true"What I’d probably do is hook into the dropdown’s OnDataBound() event, and insert the “Select an item” text at index 0.
Then, if you want to pre-select the “Mattresses” one, maybe something like this?
There could be other stuff going on here (update panels, etc.) which could throw off my suggestion. If that doesn’t work; post your code for the DropDown, as well as any code that deals with the databinding and selection logic.