Not possible to load DropDownList on FormView from code behind?

前端 未结 3 1379
旧巷少年郎
旧巷少年郎 2021-01-13 10:19

I have a UserControl, containing a FormView, containing a DropDownList. The FormView is bound to a data control.

Like so:



        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-13 10:33

    It is working for me with the OnDataBound event instead of OnDataBinding which occurs before the control is created.

    
    
    

    Code-behind:

    Protected Sub ddlCountry_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
    
        Dim ddlCountry As New DropDownList
        Dim strCountry As String = String.Empty
        Dim lstItemToFind As New ListItem
    
        ddlCountry = FormViewUsers.FindControl("ddlCountry")
        strCountry = Eval("country")
        lstItemToFind = ddlCountry.Items.FindByValue(strCountry)
    
        If (Not IsNothing(lstItemToFind)) Then
            ddlCountry.SelectedValue = strCountry
        End If
    
    End Sub
    

    If you use this on other templates than the Edit you just need to validate:

        If (FormViewUsers.CurrentMode = FormViewMode.Edit) Then
    
        End If
    

提交回复
热议问题