Exemple #1
0
        /// <summary></summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnPreLoad(object sender, EventArgs e)
        {
            IEntity entity = null;

            try
            {
                entity = Entity;
            }
            catch (XCodeException ex)
            {
                // 由下面自行处理,而不是抛出这个异常
                if (!ex.Message.Contains("参数错误!无法取得编号为"))
                {
                    throw;
                }
            }
            // 判断实体
            if (entity == null)
            {
                String msg = null;
                Object eid = EntityID;
                if (IsNew)
                {
                    msg = String.Format("参数错误!无法取得编号为{0}的{2}({1})!可能未设置自增主键!", eid, Factory.TableName, Factory.Table.Description);
                }
                else
                {
                    msg = String.Format("参数错误!无法取得编号为{0}的{2}({1})!", eid, Factory.TableName, Factory.Table.Description);
                }

                WebHelper.Alert(msg);
                Response.Write(msg);
                Response.End();
                return;
            }

            Control btn     = SaveButton;
            Control btncopy = CopyButton;

            if (!Page.IsPostBack)
            {
                if (ManagePage != null && ManagePage.Container != null && ManagePage.ValidatePermission)
                {
                    CanSave = entity.IsNullKey && ManagePage.Acquire(PermissionFlags.Insert) || ManagePage.Acquire(PermissionFlags.Update);

                    // 复制只需要新增权限
                    if (btncopy != null)
                    {
                        btncopy.Visible = ManagePage.Acquire(PermissionFlags.Insert);
                    }
                }

                // 新增数据时,不显示复制按钮
                if (IsNew && btncopy != null)
                {
                    btncopy.Visible = false;
                }

                if (btn != null)
                {
                    btn.Visible = CanSave;

                    if (btn is IButtonControl)
                    {
                        (btn as IButtonControl).Text = entity.IsNullKey ? "新增" : "更新";
                    }
                }

                //利用js控制按钮点击状态
                //2013-1-14 @宁波-小董,注释下面2句:
                //原因:这里在XCode默认网站后台没有问题,但在其他网站后台,如果利用js对表单进行验证,就会出现错误,
                //验证不通过,这里也会执行js代码,“正在提交”,然后页面就死掉了
                //不知道要怎么修改才能使得页面验证不通过时,这个就不执行。
                //RegButtonOnClientClick(btn);
                //RegButtonOnClientClick(btncopy);

                SetForm();
            }
            else
            {
                // 如果外部设置了按钮事件,则这里不再设置
                if (btn != null && btn is IButtonControl && ControlHelper.FindEventHandler(btn, "Click") == null)
                {
                    (btn as IButtonControl).Click += delegate
                    {
                        GetForm();
                        if (ValidForm())
                        {
                            SaveFormWithTrans();
                        }
                    }
                }
                ;
                // 这里还不能保存表单,因为使用者习惯性在Load事件里面写业务代码,所以只能在Load完成后保存
                //else if (Page.AutoPostBackControl == null)
                //{
                //    GetForm();
                //    if (ValidForm()) SaveFormWithTrans();
                //}
                if (btncopy != null && btncopy is IButtonControl && ControlHelper.FindEventHandler(btncopy, "Click") == null)
                {
                    (btncopy as IButtonControl).Click += delegate
                    {
                        GetForm();

                        // 清空主键,变成新增
                        IEntityOperate eop = EntityFactory.CreateOperate(Entity.GetType());
                        foreach (var item in eop.Fields)
                        {
                            if (item.PrimaryKey || item.IsIdentity)
                            {
                                Entity[item.Name] = null;
                            }
                        }

                        if (ValidForm())
                        {
                            SaveFormWithTrans();
                        }
                    }
                }
                ;
            }
        }