开发者

How to conditionally load edit template in telerik rad grid?

Is there a way to conditionally load different user c开发者_运维技巧ontrol as edit form based on some user action (Edit vs. Create)?


This is how I ended up doing it

  protected void Page_Init(object sender, EventArgs e)
        {
            ctlBenefitLimitsGrid.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.WebUserControl;
            ctlBenefitLimitsGrid.ItemDataBound += BenefitLimitsGrid_ItemDataBound;
            ctlBenefitLimitsGrid.ItemCommand += BenefitLimitsGrid_ItemCommand;

            ctlBenefitLimitsGrid.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.WebUserControl;
        }

        void BenefitLimitsGrid_ItemCommand(object source, GridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Edit":
                    e.Item.OwnerTableView.IsItemInserted = false;
                    ctlBenefitLimitsGrid.MasterTableView.EditFormSettings.UserControlName =
                        @"UserControls/BenefitLimitEdit.ascx";

                    break;
                case "Add":
                    {
                        InsertAddControl(e);
                        break;
                    }
                case "Delete":
                    var benefitLimitId = Convert.ToInt32(e.CommandArgument);
                    Presenter.Delete(benefitLimitId);
                    Presenter.LoadView();
                    break;

                case "Save":
                    IEditView item;

                    if (e.Item.GetType() == typeof(GridEditFormInsertItem))
                    {
                        item =
                            (IEditView)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
                    }
                    else
                    {
                        item = ((GridDataItem)e.Item).EditFormItem.FindControl(GridEditFormItem.EditFormUserControlID) as IEditView;
                    }
                    if (item != null && item.HasErrors)
                    {
                        e.Canceled = true;
                        return;
                    }

                    e.Item.Edit = false;
                    ctlBenefitLimitsGrid.MasterTableView.ClearEditItems();
                    Presenter.LoadView();

                    break;
                case "SaveAndNew":
                    {
                        var benefitLimitCreate = (IBenefitLimitCreate)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

                        if (benefitLimitCreate.HasErrors)
                        {
                            e.Canceled = true;
                            return;
                        }

                        Presenter.LoadView();
                        InsertAddControl(e);

                        break;
                    }
            }
        }

        private void InsertAddControl(GridCommandEventArgs e)
        {
            ctlBenefitLimitsGrid.MasterTableView.ClearEditItems();

            ctlBenefitLimitsGrid.MasterTableView.EditFormSettings.UserControlName = @"UserControls/BenefitLimitCreate.ascx";

            e.Item.OwnerTableView.InsertItem();

            var insertedItem = e.Item.OwnerTableView.GetInsertItem();

            var ctlBenefitLimitCreate = (BenefitLimitCreate)insertedItem.FindControl(GridEditFormItem.EditFormUserControlID);

            ctlBenefitLimitCreate.Presenter.LoadView();
        }


I think that there was an article in the Telerik online documentation (under Insert/Update/Delete -> HowTo chapter) which does exactly what you are after.

Dick

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜