views:

223

answers:

2

Is there a way to conditionally load different user control as edit form based on some user action (Edit vs. Create)?

A: 

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

Dick Lampard
A: 

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