RadGrid 处于编辑模式,带有按需加载的 RadComboBox

RadGrid in edit mode with RadComboBox with load on demand

我有一个 Telerik RadGrid,我在其中放入 EditItemTemplate 一个 RadComboBox (ID:DdlProducts) 以加载 (按需过滤) 一组选项。

除了我尝试编辑一行之外,一切正常:

由于启用了 LoadOnDemand,我无法预设 RadComboBox 的选定值。

我遵循了 Telerik 网站上的一些建议和示例,但显然 none 可以按预期工作。

这是我的代码。

<telerik:RadGrid ID="GrdTopTen" runat="server" OnNeedDataSource="GrdTopTen_NeedDataSource" AutoGenerateColumns="false"
    ShowStatusBar="true" OnUpdateCommand="GrdTopTen_UpdateCommand" OnInsertCommand="GrdTopTen_InsertCommand" OnDeleteCommand="GrdTopTen_DeleteCommand" OnItemDataBound="GrdTopTen_ItemDataBound"
    AllowAutomaticUpdates="false" AllowAutomaticInserts="false" AllowAutomaticDeletes="false">
    <MasterTableView AllowFilteringByColumn="false" EditMode="InPlace" DataKeyNames="TopTenID" CommandItemDisplay="TopAndBottom">
        <Columns>
            <telerik:GridBoundColumn DataField="TopTenID" DataType="System.Int32" HeaderText="ID" ReadOnly="True" UniqueName="TopTenID" Visible="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ProdID" DataType="System.Int32" UniqueName="ProdID" Visible="false">
            </telerik:GridBoundColumn>
            <telerik:GridNumericColumn AllowOutOfRangeAutoCorrect="true" DataField="TopTenPosition" DataType="System.Int32" MaxValue="25" MinValue="1" DecimalDigits="0" ShowSpinButtons="true"></telerik:GridNumericColumn>
            <telerik:GridTemplateColumn DataField="Title" HeaderText="Titolo" UniqueName="Title">
                <ItemTemplate>
                    <%# Eval("Title") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadComboBox RenderMode="Lightweight" runat="server" ID="DdlProducts" DataTextField="Title" DataValueField="ProdID" AutoPostBack="true" Width="350px"
                        EmptyMessage="Selezionare un titolo..." ItemsPerRequest="20" MinFilterLength="3" Filter="Contains"
                        EnableLoadOnDemand="True" HighlightTemplatedItems="true"
                        OnSelectedIndexChanged="DdlProducts_SelectedIndexChanged" OnItemsRequested="DdlProducts_ItemsRequested">
                    </telerik:RadComboBox>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridButtonColumn CommandName="Delete" Text="Elimina" UniqueName="DeleteColumn" ButtonType="FontIconButton"
                ConfirmText="Sei sicuro di voler cancellare questa voce?" ConfirmDialogType="RadWindow" />
            <telerik:GridEditCommandColumn FooterText="EditCommand footer" UniqueName="EditCommandColumn"
                HeaderText="Modifica" HeaderStyle-Width="100px" ButtonType="FontIconButton">
            </telerik:GridEditCommandColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

隐藏代码:

protected void GrdTopTen_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.IsInEditMode)
    {
        GridEditableItem item = (GridEditableItem)e.Item;
        if (!(e.Item is IGridInsertItem))
        {
            RadComboBox combo = RadComboBox)item.FindControl("DdlProducts");

            RadComboBoxItem preselectedItem = new RadComboBoxItem();

            preselectedItem.Text = item["Title"].Text;
            preselectedItem.Value = item["ProdID"].Text;
            //the above lines doesn't contains any value, are empty.

            combo.Items.Insert(0, preselectedItem);
            combo.SelectedIndex = 0;
        }
    }
}

protected void GrdTopTen_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    using (EcommerceEntities db = new EcommerceEntities())
    {
        var prods = from ttp in db.TopTenProducts
                    join p in db.Products on ttp.ProdID equals p.prodID
                    join pd in db.ProductDescriptions on p.prodID equals pd.prodID
                    where p.prodParent == null && pd.langID==1
                    orderby ttp.TopTenPosition
                    select new
                    {
                        ProdID = p.prodID,
                        Title = pd.prodName,
                        ISBN = p.prodISBN,
                        ttp.TopTenPosition,
                        ttp.TopTenID
                    };
        GrdTopTen.DataSource = prods.ToList();
    }
}

protected void DdlProducts_ItemsRequested(object sender, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e)
{
    using (EcommerceEntities db = new EcommerceEntities())
    {
        string filter = e.Text;
        var prods = from p in db.Products
                    join pd in db.ProductDescriptions on p.prodID equals pd.prodID
                    where p.prodParent == null && pd.langID == 1 && pd.prodName.Contains(e.Text)
                    select new
                    {
                        ProdID = p.prodID,
                        Title = pd.prodName,
                        ISBN = p.prodISBN
                    };

        RadComboBox comboBox = (RadComboBox)sender;
        // Clear the default Item that has been re-created from ViewState at this point.
        comboBox.Items.Clear();

        foreach (var p in prods)
        {
            RadComboBoxItem item = new RadComboBoxItem();
            item.Text = p.Title;
            item.Value = p.ProdID.ToString();
            item.Attributes.Add("ISBN", p.ISBN);
            comboBox.Items.Add(item);
            item.DataBind();
        }

    }
}

我使用的是 2017 年第一季度版的 Telerik UI ASP.NET

最后,我通过使用 DataBinder.Eval 方法检索行值解决了问题:

preselectedItem.Text = DataBinder.Eval(item.DataItem, "Title").ToString();                
preselectedItem.Value = DataBinder.Eval(item.DataItem, "ProdID").ToString();

令人难以置信的是 Telerik 的在线文档有多么错误!