dropdown
-
I have populated a dropdown control. The items inside the dropdown are: "hello", "you", "there" based on a few retrieved items from the database, I would like the correct item in the dropdown to be selected. For example: string strValue = "you"; ddlgvSources.selectedItem.Text = strValue; The problem here is that the text in strValue over writes the value inside the dropdown, so when I click on the dropdown to see all the items, it shows: you you there Do you know why? Thanks
-
I have populated a dropdown control. The items inside the dropdown are: "hello", "you", "there" based on a few retrieved items from the database, I would like the correct item in the dropdown to be selected. For example: string strValue = "you"; ddlgvSources.selectedItem.Text = strValue; The problem here is that the text in strValue over writes the value inside the dropdown, so when I click on the dropdown to see all the items, it shows: you you there Do you know why? Thanks
You are trying to replace the text instead of selecting the required one. You have to find the correct list item using the method
FindItemByText
orFindItemByValue
of DropDownand then Select it.ListItem item;
ddl.SelectedIndex = -1;
item = ddl.Items.FindByText("hello");
if (item != null) item.Selected = true; -
You are trying to replace the text instead of selecting the required one. You have to find the correct list item using the method
FindItemByText
orFindItemByValue
of DropDownand then Select it.ListItem item;
ddl.SelectedIndex = -1;
item = ddl.Items.FindByText("hello");
if (item != null) item.Selected = true;ListItem item=DropDownList1.Items[DropDownList1.SelectedIndex]; DropDownList1.Items.Remove(item); DropDownList1.Items.Insert(0, item);
-
ListItem item=DropDownList1.Items[DropDownList1.SelectedIndex]; DropDownList1.Items.Remove(item); DropDownList1.Items.Insert(0, item);
And how does this solved the OPs problem?
I know the language. I've read a book. - _Madmatt
-
You are trying to replace the text instead of selecting the required one. You have to find the correct list item using the method
FindItemByText
orFindItemByValue
of DropDownand then Select it.ListItem item;
ddl.SelectedIndex = -1;
item = ddl.Items.FindByText("hello");
if (item != null) item.Selected = true; -
I have populated a dropdown control. The items inside the dropdown are: "hello", "you", "there" based on a few retrieved items from the database, I would like the correct item in the dropdown to be selected. For example: string strValue = "you"; ddlgvSources.selectedItem.Text = strValue; The problem here is that the text in strValue over writes the value inside the dropdown, so when I click on the dropdown to see all the items, it shows: you you there Do you know why? Thanks
Try this:
ddlgvSources.SelectedValue = strValue;
-
Try this:
ddlgvSources.SelectedValue = strValue;