开发者

How to show value of sqldatasource and value from code to dropdownlist

开发者 https://www.devze.com 2022-12-15 05:30 出处:网络
I have a dropdown within gridview i have binded that dropdown withdatasource values. i will select any value from dropdown then it will save in database.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消