コード例 #1
0
    //Save Data
    private void SaveData()
    {
        DictionaryItem dictionaryItem = new  DictionaryItem();
        bool           flag           = true;

        try
        {
            if (_actionMode == Mode.AddChild)
            {
                dictionaryItem.GroupCode = txtGroupCode.Text.Trim();
            }
            else if (_actionMode == Mode.New)
            {
                dictionaryItem.GroupCode = DictionaryItem.Root.ItemCode;
            }

            dictionaryItem.ItemCode = txtCode.Text.Trim();
            dictionaryItem.Name     = txtName.Text.Trim();
            dictionaryItem.ItemType = Cast.Enum <DictionaryItemType>(rdlItemType.SelectedValue);
            switch (dictionaryItem.ItemType)
            {
            case DictionaryItemType.Boolean:
                dictionaryItem.BoolValue = Cast.Bool(this.ddlBoolValue.SelectedValue);
                break;

            case DictionaryItemType.Numric:
                dictionaryItem.NumberValue = Cast.Decimal(this.txtNumberValue.Text);
                break;

            case DictionaryItemType.String:
                if (this.txtStringValue.Value.Trim().Length > 1000)
                {
                    dictionaryItem.StringValue = this.txtStringValue.Value.Trim().Substring(0, 1000);
                }
                else
                {
                    dictionaryItem.StringValue = this.txtStringValue.Value;
                }
                break;

            default:
                break;
            }
            dictionaryItem.Note = txtNote.Value.Trim();
            using (_session = new Session())
            {
                if (IsAddNew())
                {
                    if (DictionaryItem.Exists(_session, new string[] { "ItemCode" }, new object[] { dictionaryItem.ItemCode }))
                    {
                        throw new ApplicationException(string.Format("代码:{0}已经存在;如果是子项,建议将组代码作为前缀", dictionaryItem.ItemCode));
                    }
                    flag = dictionaryItem.Create(_session);
                }
                else
                {
                    dictionaryItem.ItemCode = (this.hidItemCode.Value);
                    flag = dictionaryItem.Update(_session, "ItemCode", "Name", "BoolValue", "NumberValue", "StringValue");
                }
            }
            this.hidItemCode.Value = dictionaryItem.ItemCode.ToString();
            if (flag)
            {
                WebUtil.ShowMsg(this, "操作成功", "提示");
            }
            else
            {
                WebUtil.ShowMsg(this, "操作失败", "提示");
            }
        }
        catch (UnauthorizedException ex)
        {
            WebUtil.ShowMsg(this, ex.Message, "警告");
        }
        catch (ApplicationException ex)
        {
            WebUtil.ShowMsg(this, ex.Message, "提示");
        }
        catch (Exception ex)
        {
            logger.Info("保存DictionaryItem", ex);
            WebUtil.ShowMsg(this, "发生未处理的异常,请刷新页面重新操作,或者联系系统管理员");
        }
    }