I am trying to achieve the following:
-
Eliminate the
FormView’sdefault edit link button. -
Display the
FormViewin edit mode onPage_Loadwith current data and additional DDL data selections.
Currently I have the following to bind the DDL to the current database value based upon id = x etc:
<asp:DropDownList ID="DropDownList1" runat="server"
SelectedValue='<%# Bind("xxx") %>'
DataSourceID="xxx"
DataTextField="xxx"
DataValueField="xxx"
ondatabound="DropDownList1_DataBound">
Via C# code I do this to add additional DDL list items for selection: (which is a waste of my energy because the data is already in the database!)
protected void DropDownList1 _DataBound(object sender, EventArgs e)
{
DropDownList DropDownList1 = (DropDownList)FormView1.FindControl("DropDownList1");
DropDownList1.Items.Insert(1, new ListItem("0 - 1 km/h", "0"));
DropDownList1.Items.Insert(2, new ListItem("2 - 5 km/h", "1"));
DropDownList1.Items.Insert(3, new ListItem("6 - 11 km/h", "2"));
DropDownList1.Items.Insert(4, new ListItem("12 - 19 km/h", "3"));
DropDownList1.Items.Insert(5, new ListItem("20 - 28 km/h", "4"));
}
There has got to be a better way!
Dont know exactly how you are connecting to the database but if you are using datacontext linq to sql is a good way to do it. Your ‘ddl’ will have an onLoad event and you can use a query in the code behind. For example:
And then in the code behind: