Home
 Index > ASP.NET Web Applications > How to select a dropdownlist item in an EditIte...

ASP.NET Web Applications:  Databinding , FormView , ObjectDataSource

How to select a dropdownlist item in an EditItemTemplate of a FormView that is bound to an ObjectDatasource

Get the data returned from an ObjectDataSource and select an option in a dropdownlist from the code-behind.

Added on 12 Jul 2008

General Solutions:

Solution Summary:
If the web control is in the EditItemTemplate, in the FormView's DataBound event, check that the current mode is "Edit", find the control, and select the option based on the DataItem with the appropriate property.
Solution Details:

The following code checks the current mode, and if it's "Edit" mode, finds the dropdownlist control and selects the option with the value in the DataItem:

    Private Sub fv_Member_DataBound(ByVal sender As ObjectByVal e As System.EventArgs) Handles fv_Member.DataBound
        
If fv_Member.CurrentMode = FormViewMode.Edit Then
            Dim drv As DataRowView = fv_Member.DataItem
            
Dim ddlTitle As DropDownList = CType(fv_Member.Row.FindControl("DdlTitle"), DropDownList)
            ddlTitle.ClearSelection()
            
If Not ddlTitle.Items.FindByValue(drv.Item("Title")) Is Nothing Then
                ddlTitle.Items.FindByValue(drv.Item("Title")).Selected = True
            End If
        End If
    End Sub
Was this solution useful? Yes No Added on 12 Jul 2008
Rating: 

Copyright 2010 © E-Centric, Inc. | Terms of Use