DataBind or DataBound
I thought I had a simple task; add "Select..." to several dropdown lists.
However I am not getting the results I want and I am getting more and more confused if I should be using the dataBound or dataBinding event in my Gridview edit mode.
My code is pretty simple;
开发者_如何转开发protected void ActivityList_DataBinding (object sender, System.EventArgs e)
{
DropDownList ddl2 = (DropDownList)(sender);
var act = Eval("myactivity").ToString();
if (act != "") { ddl2.SelectedValue = act; }
ddl2.Items.Insert(0, new ListItem("Select..", "-1"));
}
This checks if a value has already been selected, and would hopefully jump to the selection if it has been, still adding a Select item to the list.
Using the dataBound event works in the sense it addes my Select, but does not go to the selected value if there is one. Also it is creating odd behavior, jumping to the top of my page upon selection, rather than staying on the row I am editing.
Using dataBinding does not show my added items at all.
All advice welcome!
Alex (Lost in CodeLand)
Set your AppendDataBoundItems=True
in your DDL. If you are calling the databind method in code you will want to clear the items and readd the new listitem before databinding.
精彩评论