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

时间:2018-09-21 23:30:40

标签: c# asp.net webforms telerik radgrid

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

一切正常,除非我尝试编辑行:

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

我在Telerik网站上遵循了一些建议和示例,但显然没有一个能按预期工作。

这是我的代码。

<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

1 个答案:

答案 0 :(得分:0)

最后,我已经通过使用 DataBinder.Eval 方法检索行值来解决:

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

Telerik的在线文档如此令人难以置信!

相关问题