Not possible to load DropDownList on FormView from code behind?

前端 未结 3 1343
旧巷少年郎
旧巷少年郎 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.

    <asp:DropDownList ID="ddlCountry" runat="server" DataSourceID="SqlDataSrcCountries" AutoPostBack="True" DataTextField="name" DataValueField="code" OnDataBound="ddlCountry_DataBound">
    </asp:DropDownList>
    

    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
    
    0 讨论(0)
  • 2021-01-13 10:37

    Implement the OnDataBinding directly on your DropDownList.

    When you bind your formview to some data the OnDataBinding for the DropDownList will fire. At this point you can then load the the values you want into the list and bind the selected value to the loaded list as well.

    Here is an example:

    <asp:DropDownList ID="ParentMetricCode"  runat="server" OnDataBinding="ParentMetricCode_DataBinding" />
    

    Then implement the OnDataBinding:

    protected void ParentMetricCode_DataBinding(object sender, System.EventArgs e)
    {            
        DropDownList ddl = (DropDownList)(sender);
    
        // Fill the list items however you want
        ddl.Items.Add(new ListItem("1", "1"));
        ddl.Items.Add(new ListItem("2", "2"));
        // etc...
    
        // Set the selected value
        ddl.SelectedValue = Eval("ParentMetricCode").ToString();
    }
    

    When DataBind your FormView, everything will start to do its magic :)

    Also if you loading the list data for your DropDownList from a DB or something you might want to cache it since each 'row' of data will cause the loading of the list data.

    EDIT: Since you think it's not possible I wrote a quick demo app to prove how it works:

    In your aspx file include this:

    <asp:FormView ID="fvTest" runat="server">
        <ItemTemplate>
            <asp:DropDownList ID="ddlTest" runat="server" OnDataBinding="ddlTest_DataBinding"></asp:DropDownList>
        </ItemTemplate>
    </asp:FormView>
    

    Then in your .cs file:

    public class MockData
    {
        public string ID { get; set; }
        public string Text { get; set; }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        List<MockData> lst = new List<MockData>();
        lst.Add(new MockData() { ID = "3", Text = "Test3" });
        fvTest.DataSource = lst;
        fvTest.DataBind();
    }
    
    protected void ddlTest_DataBinding(object sender, System.EventArgs e)
    {
        DropDownList ddl = (DropDownList)(sender);
        ddl.Items.Add("1");
        ddl.Items.Add("2");
        ddl.Items.Add("3");
        ddl.Items.Add("4");
        ddl.Items.Add("5");
    
        ddl.SelectedValue = Eval("ID").ToString();
     }
    

    Run the code... the DropDownList will be loaded with all the values and be set to the correct selected value that was set in the mocked object.

    Not sure what else I can do to prove it any better... I think your missing how DataBinding actually works. If you try to do it at the FormView level, the controls don't get made until the FormView is actually bound. When the binding happens, you can trigger each control within the template to do something by implementing it's OnDataBinding event. At that point the current iteration of bound object and it's values are available and that is where you see my code do an Eval("ID").

    0 讨论(0)
  • 2021-01-13 10:45

    Ok, found "a" solution:
    1. Call the DDL populate function from the FormView_ItemCreated event.
    2. Modify the code to populate the DDL like so:

    Public Overloads Shared Sub BindListControl(Of theLLBLgenEntityType As EntityBase) _   
            (ByVal formView As FormView, _
            ByVal listControlName As String, _
            ByVal theCollection As CollectionCore(Of theLLBLgenEntityType), _
            ByVal DataValueField As EntityField, _
            ByVal DataTextField As EntityField, _
            ByVal IncludeEmptyItem As Boolean)
    
        If formView.CurrentMode = FormViewMode.Edit Then
            Dim theListcontrol As System.Web.UI.WebControls.ListControl
            theListcontrol = CType(formView.FindControl(listControlName), System.Web.UI.WebControls.ListControl)
    
        For Each entity As theLLBLgenEntityType In theCollection
            theListcontrol.Items.Add(New ListItem(entity.Fields(DataTextField.Name).CurrentValue.ToString, entity.Fields(DataValueField.Name).CurrentValue.ToString))
        Next
    
        If IncludeEmptyItem Then 
            theListcontrol.Items.Insert(0, New ListItem("", ""))
        End If
    End Sub
    

    It kinda seems to basically boil down to, you cannot do additional "Databinding", programatically at runtime, within a FormView.

    (Anyone know why the formatting of my code is all screwed up in this post??)

    Update

    This solution raises yet another batch of issues, related to inserting new items. After a lot of screwing around I eventually found a way around that. At this point, I am basically at the point where I would recommend either avoiding the FormView control entirely, or definitely avoid programatically altering bound controls. Hopefully, perhaps using seperate data controls for each DropDownList might work better, but this might be wishful thinking as well.

    0 讨论(0)
提交回复
热议问题