How to show value of sqldatasource and value from code to dropdownlist
I have a dropdown within gridview i have binded that dropdown with datasource values.
i will select any value from dropdown then it will save in database.
now i want to show the database value in the top value in same dropdown with sqldatasource.
Suppose i have value in dropdown from sqldatasource is test,test1,test2.
Suppose i have selected test1 then it will save in database now i want that dropdown show this values开发者_运维百科 test1,test,test1,test2
Please help me in this problem
Try placing your insert code within the DataBound event. Something like this:
protected void myDropDownList_DataBound(object sender, EventArgs e)
{
DropDownList dropDownList1 = (DropDownList)sender;
dropDownList1.Items.Insert(0, new ListItem(newItem.GetText(), newItem.GetValue()));
}
In the code above, newItem is just a static method that uses the database to get the latest item inserted. You can obviously use your own method/variables for that bit, but basically the ListItem method will create a new item for your drop down list.
精彩评论