I have a drop down list that is bound to some items. i want to replace the selected item with a text box value and again want to bind the dropdownlist with new values.
For this i am currently storing the dropdown list items in a temporary List. How can I replace the current selected item with textbox value.
for (int i = 0; i < DropDownEmail.Items.Count; i++)
{
if (?)
{
ObjRegistration = new ClassRegistration();
ObjRegistration.UserName = TextBoxEmail.Text;
tempEmailList.Add(ObjRegistration)
}
else{
ObjRegistration = new ClassRegistration();
ObjRegistration.UserName = DropDownEmail.Items[i].Text;
tempEmailList.Add(ObjRegistration);
}
}
Your code doesn’t make much sense as it is written now, but in general, if you want to replace an item in a dropdown list you need to do something like this:
In your case, it seems that you are binding to a custom collection of
ClassRegistrationbut since you are doing this on code-behind, once you bind the elements to the Dropdown list for the first time, you only have a reference to theItemscollection in the dropdown which are all of typeListItem.You can, alternatively, update your underlying custom collection and rebind that to the dropdown list: