I have a dropdownlist in my EditFormTemplate. In the code for OnItemCreated I have the following:
If TypeOf e.Item Is GridEditableItem And e.Item.IsInEditMode Then
'create oDataTable
Dim ddl As DropDownList = TryCast(e.Item.FindControl("ddOfficeID"), DropDownList)
ddl.DataSource = oDataTable
ddl.DataTextField = "OfficeName"
ddl.DataValueField = "OfficeID"
ddl.DataBind()
End If
In the code for OnUpdateCommand I have the following:
Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
Dim PersonnelID As String = editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("PersonnelID").ToString()
'get other values...
Dim OfficeID As String = (TryCast(editedItem.FindControl("ddOfficeID"), DropDownList)).Text
strSQL = "UPDATE tPersonnel SET [OtherValues] = '" & OtherValues& "', [OfficeID] = " & OfficeID & " WHERE ([PersonnelID] = " & PersonnelID & ")"
'update database...
So far my dropdownlist is bound when the grid goes into edit mode and the database is updated when the Update button is clicked.
My only problem is that when the edit form is loaded the dropdownlist does not reflect the original selected value. When I tried adding SelectedValue='<%# Eval("OfficeID")%>' to my dropdownlist, it generates the following error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Since the dropdown is being bound on ItemCreated, I'm confused by that error. I've seen some other threads about this, but frankly they were over my head. Can someone explain in super simple terms how to set the selectedvalue?
Thanks!