Hi,
I've got a dropdownlist that is data binded to a table thus all the values are from the table. But how can I display the dropdown list with my own values and the table's values?
Regards,
Roger
Printable View
Hi,
I've got a dropdownlist that is data binded to a table thus all the values are from the table. But how can I display the dropdown list with my own values and the table's values?
Regards,
Roger
Yes you can add items to a dropdownlist after it has been bound in a couple of different ways.
ie.Code://Populate Sector Combo
this.ddlSector.DataSource = flaxwebnet.BusinessLogic.Sector.Combo();
this.ddlSector.DataTextField = "Value";
this.ddlSector.DataValueField = "ID";
this.ddlSector.DataBind();
//Add at the end
this.ddlSector.Items.Add("Test_end");
//Insert in a position
this.ddlSector.Items.Insert(0, "Test_insert");
And if you want to add both Text and Value:
ddList.Items.Add(new ListItem("Item1", "value1"));
Thanks a lot guys, I got it to work. However, I got one question about drop down list.
For example, in the dropdown list, when the user chooses "Item1", it will be added to the database. When I want to display the drop down list, the selected item will be "Item1". But it could not work.
Coding:
VB Code:
cmd.commandtext = [select statement] str = cmd.executescalar () dropdownlist.selectedvalue.toString = str
you might have to loop through all items in the dropdownlist.
But is my coding correct? I'm very sure that my SQL statement is correct but I am not sure about the selectedValue.toString()
If you are trying to set which item in the dropdownlist is selected, there are functions in there to do that. Look at DropDownList.Items.FindByValue. Kind of like:Quote:
Originally Posted by Aka Roger
VB Code:
MyDDL.SelectedIndex = MyDDL.Items.IndexOf(MyDDL.Items.FindByValue(MyString))
Thanks a lot. I got it to work without looping it.