protected void bt_AddDetail_Click(object sender, EventArgs e)
    {
        ListTable<CM_ContractDetail> _details = ViewState["Details"] as ListTable<CM_ContractDetail>;
        DateTime begindate = new DateTime();
        DateTime enddate = new DateTime();
        TextBox tbx_BeginDate = pl_detail.FindControl("CM_Contract_BeginDate") == null ? null : (TextBox)pl_detail.FindControl("CM_Contract_BeginDate");
        TextBox tbx_EndDate = pl_detail.FindControl("CM_Contract_EndDate") == null ? null : (TextBox)pl_detail.FindControl("CM_Contract_EndDate");
        if (tbx_BeginDate != null && tbx_EndDate != null)
        {
            DateTime.TryParse(tbx_BeginDate.Text, out begindate);
            DateTime.TryParse(tbx_EndDate.Text, out enddate);
            if (enddate <= begindate)
            {
                MessageBox.Show(this, "合同终止日期不能小于起始日期。");
                return;
            }
        }

        if (ddl_PayMode.SelectedValue == "0")
        {
            MessageBox.Show(this, "付款周期必选!");
            return;
        }

        if (ddl_BearMode.SelectedValue == "2" && ddl_PayMode.SelectedValue == "1")
        {
            MessageBox.Show(this, "付款周期为每月的不支付预提!");
            return;
        }
        decimal displaycount = 0;
        if (!decimal.TryParse(txt_count.Text.Trim(), out displaycount))
        {
            MessageBox.Show(this, "请正确填写数量!");
            return;
        }
        CM_ContractDetail item;
        if (ViewState["Selected"] == null)
        {
            if (ddl_AccountTitle.SelectedValue != "220" && ddl_AccountTitle.SelectedValue != "224")
            {
                IList<CM_ContractDetail> _list = (_details.GetListItem()).Where(p => p.AccountTitle == int.Parse(ddl_AccountTitle.SelectedValue)).ToList();
                //新增科目
                if (_details.GetListItem().Where(p => p.AccountTitle == int.Parse(ddl_AccountTitle.SelectedValue)).Count() > 0)
                {
                    MessageBox.Show(this, "该科目已添加!");
                    return;
                }
            }
            item = new CM_ContractDetail();
            ViewState["MAXID"] = ((int)ViewState["MAXID"]) + 1;
            item.ID = (int)ViewState["MAXID"];
            item.AccountTitle = int.Parse(ddl_AccountTitle.SelectedValue);
            if (TreeTableBLL.GetChild("MCS_PUB.dbo.AC_AccountTitle", "ID", "SuperID", item.AccountTitle).Rows.Count > 0)
            {
                MessageBox.Show(this, "费用科目必须选择最底级会计科目!" + ddl_AccountTitle.SelectedItem.Text);
                return;
            }
        }
        else
        {//修改科目

            item = _details[ViewState["Selected"].ToString()];
        }
        CM_Contract _m = new CM_Contract();
        pl_detail.GetData(_m);

        item.BearMode = int.Parse(ddl_BearMode.SelectedValue);
        item.FeeCycle = GetContractcycle();
        item.Amount = decimal.Parse(tbx_Amount.Text);
        item.BearPercent = decimal.Parse(tbx_BearPercent.Text);
        item.ApplyLimit = decimal.Parse(tbx_ApplyLimit.Text);
        item["DiaplayCount"] = txt_count.Text.Trim();
        item["ISCA"] = ddl_YesNO.SelectedValue;
        if (item.Amount == 0 || item.BearPercent == 0 || item.ApplyLimit == 0)
        {
            MessageBox.Show(this, "对不起,合同总额(元),每次付款金额(元),占月目标销售额比不能为0!");
            return;
        }
        item.PayMode = int.Parse(ddl_PayMode.SelectedValue);
        item.Remark = tbx_Remark.Text;

        #region 获取已选择关联的品牌
        if (cbl_Brand.Visible)
        {
            if (cbl_Brand.SelectedIndex < 0)
            {
                MessageBox.Show(this, "对不起,请选择该费用关联的产品品牌!");
                return;
            }

            item["RelateBrand"] = "";
            foreach (ListItem i in cbl_Brand.Items)
            {
                if (i.Selected) item["RelateBrand"] += i.Value + ",";
            }
        }
        #endregion

        if (ViewState["Selected"] == null)
            _details.Add(item);
        else
            _details.Update(item);

        gv_Detail.SelectedIndex = -1;
        BindGrid();

        tbx_Amount.Text = "0";
        tbx_BearPercent.Text = "100";
        tbx_ApplyLimit.Text = "0";
        tbx_Remark.Text = "";
        bt_AddDetail.Text = "新增";
        ViewState["Selected"] = null;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            #region 获取页面参数
            ViewState["ContractID"] = Request.QueryString["ContractID"] == null ? 0 : int.Parse(Request.QueryString["ContractID"]);
            if (Request.QueryString["ClientID"] != null)
            {
                ViewState["ClientID"] = Int32.Parse(Request.QueryString["ClientID"]);
                Session["ClientID"] = ViewState["ClientID"];
            }
            else if (Session["ClientID"] != null)
            {
                ViewState["ClientID"] = Int32.Parse(Session["ClientID"].ToString());
            }

            if ((int)ViewState["ContractID"] == 0 && (int)ViewState["ClientID"] == 0) Response.Redirect("~/SubModule/DeskTop.aspx");
            #endregion

            BindDropDown();

            #region 创建空的列表
            ListTable<CM_ContractDetail> _details = new ListTable<CM_ContractDetail>(new CM_ContractBLL((int)ViewState["ContractID"]).Items, "AccountTitle");
            ViewState["Details"] = _details;
            #endregion

            if ((int)ViewState["ContractID"] != 0)
            {
                BindData();
            }
            else if ((int)ViewState["ClientID"] != 0)
            {
                CM_Contract _c = new CM_Contract();
                _c.Client = (int)ViewState["ClientID"];
                _c.BeginDate = DateTime.Today;
                _c.EndDate = DateTime.Today.AddYears(1);
                _c.Classify = 21;       //物业租赁合同
                _c.ApproveFlag = 2;
                _c.State = 1;
                pl_detail.BindData(_c);

                bt_del.Visible = false;
                bt_Submit.Visible = false;
                bt_Disable.Visible = false;
                bt_FeeApply.Visible = false;
                UploadFile1.Visible = false;
            }
            else
            {
                Response.Redirect("PropertyList.aspx");
            }
        }
    }