Not possible to load DropDownList on FormView from

2020-03-24 07:01发布

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

Like so:

<asp:FormView ID="frmEdit" DataKeyNames="MetricCode" runat="server" DefaultMode="Edit" DataSourceID="llbDataSource" Cellpadding="0" >
    <EditItemTemplate>
        <asp:DropDownList ID="ParentMetricCode"  runat="server" SelectedValue='<%# Bind("ParentMetricCode") %>' />
    </EditItemTemplate>
<asp:FormView>

I am trying to populate the DropDownList from the codebehind. If this was not contained in a FormView, I would normally just do it in the Page_Load event. However, that does not work within a FormView, as as soon as I try to do it, accessing the dropdownlist in code, i.e.:

theListcontrol = CType(formView.FindControl(listControlName), ListControl)  

...the data binding mechanism of the FormView is invoked, which, of course, tries to bind the DropDownList to the underlying datasource, causing a **'ParentMetricCode' has a SelectedValue which is invalid because it does not exist in the list of items. "Parameter name: value ..." error, since the DropDownList has not yet been populated.

I tried performing the load in the DataBinding() event of the FormView, but then:

theListcontrol = CType(formView.FindControl(listControlName), System.Web.UI.WebControls.ListControl)

...fails, as the FormView.Controls.Count = 0 at that point.

Is this impossible? (I do not want to have to use a secondary ObjectDataSource to bind the dropdownlist to)

3条回答
Summer. ? 凉城
2楼-- · 2020-03-24 07:34

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").

查看更多
乱世女痞
3楼-- · 2020-03-24 07:38

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.

查看更多
Rolldiameter
4楼-- · 2020-03-24 07:46

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
查看更多
登录 后发表回答