代码之家  ›  专栏  ›  技术社区  ›  Giox

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

  •  0
  • Giox  · 技术社区  · 6 年前

    我有一个Telerik RadGrid放在 按需过滤 )一组选项。

    一切正常,但我尝试编辑行时除外:

    我无法预设RadComboBox的选定值 ,因为已启用LoadOnDemand。

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Giox    6 年前

    最后,我通过使用 方法:

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

    难以置信的是,Telerik的在线文档有多糟糕!