Override PopulateListControl in ForeignKey_Edit field template RRS feed

  • Question

  • User-1442747414 posted

    I would like to customize the list of items available in the dropdown for selection of a foreign key value. For this, I started with a copy of the ForeignKey_Edit.ascx. Inside this template, I would like to override the PopulateListControl method, or somehow control the databinding source of the dropdown.

    I tried removing the call to PopulateListControl and replaced it with my own PopulateListControl_Custom method, but this introduced an issue that the appropriate value in the dropdown is not selected, because in the OnDataBinding event, the dropdown has not been bound yet. If I manually cause the dropdown to bind in my PopulateListControl_Custom method, then I get double the items in the dropdown due to double databinding.

    What is the best way to influence the dropdownlist's datasource?

    Saturday, April 16, 2011 11:17 AM


  • User-330204900 posted

    Hi There JBliss, I generally follow the method of adding the items like this:

    protected new void PopulateListControl(ListControl listControl)
        var query = Column.Table.GetQuery();
        // row
        var entityParam = Expression.Parameter(Column.Table.EntityType, "row");
        // row => row.DataSourceID
        var columnLambda = Expression.Lambda(Expression.Property(entityParam, Column.EntityTypeProperty), entityParam);
        // Items.Select(row => row.DataSourceID)
        var selectCall = Expression.Call(typeof(Queryable), "Select", new Type[] { query.ElementType, columnLambda.Body.Type }, query.Expression, columnLambda);
        // Items.Select(row => row.DataSourceID).Distinct
        var distinctCall = Expression.Call(typeof(Queryable), "Distinct", new Type[] { Column.EntityTypeProperty.PropertyType }, selectCall);
        var result = query.Provider.CreateQuery(distinctCall);
        var r = new List<ListItem>();
        foreach (var item in result)
            if (item != null && !String.IsNullOrEmpty(item.ToString()))
                r.Add(new ListItem(item.ToString()));
        listControl.Items.AddRange(r.OrderBy(s => s.Text).ToArray());

    Hope that is some help I either create an array of ListItem and then AddRange or iterrate though and do listControl.Items.Add(item)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 16, 2011 3:04 PM