This is a migrated thread and some comments may be shown as answers.

[Solved] SelectedValue for dropdownlist in EditFormTemplate

1 Answer 157 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Cheri Verdend
Top achievements
Rank 1
Cheri Verdend asked on 24 May 2008, 02:06 AM

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! 

1 Answer, 1 is accepted

Sort by
0
Cheri Verdend
Top achievements
Rank 1
answered on 25 May 2008, 02:05 PM
I figured out the problem.  The SelectedValue='<%# Eval("OfficeID")%> ' was correct but I needed to remove the ddl.DataBind() from the OnItemCreated sub.
Tags
Grid
Asked by
Cheri Verdend
Top achievements
Rank 1
Answers by
Cheri Verdend
Top achievements
Rank 1
Share this question
or