Unable to load DropDownList into FormView from code?

I have a UserControl containing a FormView containing a DropDownList. FormView is tied to data management.

Same:

<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 a DropDownList from code. If it were not contained in FormView, I would usually do it in the Page_Load event. However, this does not work in FormView, as soon as I try to do this, access the drop-down list in the code, i.e.:

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

... the FormView data binding mechanism is called, which, of course, tries to bind DropDownList to the underlying data source, resulting in ** "ParentMetricCode" having a SelectedValue value, which is invalid because it does not exist in the list of items. The error is "Parameter Name: Value ..." because the DropDownList is not yet populated.

DataBinding() FormView, :

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

... , FormView.Controls.Count = 0 .

? ( ObjectDataSource )

+3
3

, "" :
 1. DDL FormView_ItemCreated.
 2. DDL :

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

, , " " FormView.

(- , ?)

Update

, . . , FormView, . , , DropDownList , .

+1

OnDataBinding DropDownList.

OnDataBinding DropDownList. .

:

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

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();
}

DataBind FormView, :)

, DropDownList -, , .

EDIT: , , , , :

aspx :

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

.cs:

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();
 }

... DropDownList , .

, , ... , , DataBinding . FormView, , FormView . , , - , OnDataBinding . , , Eval("ID").

+3

It works for me with the OnDataBound event instead of OnDataBinding, which happens 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 for:

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 templates other than Edit, you just need to check:

    If (FormViewUsers.CurrentMode = FormViewMode.Edit) Then

    End If
+2
source

Source: https://habr.com/ru/post/1736708/


All Articles