1

有没有办法根据某些用户操作(编辑与创建)有条件地将不同的用户控件加载为编辑表单?

4

2 回答 2

2

这就是我最终这样做的方式

  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();
        }
于 2010-08-30T17:22:58.440 回答
0

我认为 Telerik 在线文档中有一篇文章(在 Insert/Update/Delete -> HowTo 章节下)完全符合您的要求。

迪克

于 2010-05-25T06:00:54.517 回答