0

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("&reg;");
            description = regex.Replace(description, "®");
            ListItem li = new ListItem(description, dr["ItemID"].ToString());
            ddlmyItem.Items.Add(li);
        }


         ddlmyItems.SelectedIndex = 0;
    }
}
user710502
  • 11,181
  • 29
  • 106
  • 161

3 Answers3

1

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?

var item = dropDownList.Items.FindByText(yourValue);
if(item != null)
{
    dropDownList.ClearSelection();
    item.Selected = true;
}

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.

Jim B
  • 8,344
  • 10
  • 49
  • 77
  • I added the way how the list is being binded, also when I add something like what you have above.. it gives me an error on a Javascript method I have in the aspx file ("Weird") that says " Can not have multiple selected in a Drop Down list" but this drop down is unrelated.. – user710502 Aug 08 '11 at 15:01
0

you can do this by using below method

ddlmyItems.Items.FindByText("Matresses").Selected = true;

if you use this way means this will never generate an exception even value on dropdown is not available.

mayur Rathod
  • 1,184
  • 1
  • 11
  • 26
  • Not necessarily true. You'll get a NullReference exception if ddlmyItems.Items.FindByText("Matresses") is null. – Jim B Aug 08 '11 at 17:32
0

Try

comboBox1.Text = "Matresses";

How do I set the selected item in a comboBox to match my string using C#?

Community
  • 1
  • 1
MrFox
  • 4,852
  • 7
  • 45
  • 81