コード例 #1
0
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            ltlAnnouncement.Text = "";
            try
            {
                if (checkInputData())
                {
                    cls_DepartmentInformation dInfor = new cls_DepartmentInformation();
                    dInfor.Id = Convert.ToInt32(Request["D_Id"]);
                    dInfor.CreatedTime = DateTime.Now;
                    dInfor.AdministratorId = Convert.ToInt32(cboAdministrator.SelectedValue);
                    dInfor.Belong_DepartmentId = findDepartmentId(rcbDepartment.Items[0].Text);                   //Convert.ToInt32(rTree.SelectedValue);
                    dInfor.Established_Date = (dateEstablish.SelectedDate == null) ? new DateTime(1900, 1, 1) : (DateTime)dateEstablish.SelectedDate;
                    dInfor.Established_Number = txtEstaNumber.Text;
                    dInfor.Established_Signer = txtSigner.Text;
                    dInfor.IsActive = Convert.ToInt16(ckbActive.Checked);
                    dInfor.CalculateSalary = Convert.ToInt16(ckbCalSalary.Checked);
                    dInfor.LeaderId = Convert.ToInt32(cboLeader.SelectedValue);
                    dInfor.Name = txtDepartName.Text;
                    dInfor.Order = Convert.ToInt32(rtxtOrder.Text);
                    dInfor.CompanyId = Convert.ToInt16(ddlBelongCompany.SelectedValue);
                    dInfor.SeatPosition = txtSeatPosition.Text;

                    int result;
                    if (dAction==1)
                        result = dInfor.Update();
                    else
                    {
                        result = dInfor.Insert();

                        //THEM THONG TIN VE CAC VAI TRO CUA Phong Ban
                        DataTable dtDRoles = (DataTable)ViewState["dtDRoles"];
                        if (dtDRoles.Rows[0]["Id"].ToString() != "0")
                        {
                            int index = 0;
                            cls_Department_Roles dRoles;
                            while (index < dtDRoles.Rows.Count)
                            {
                                DataRow row = dtDRoles.Rows[index];
                                dRoles = new cls_Department_Roles();
                                dRoles.DepartmentId = result;
                                dRoles.Describle = row["Describle"].ToString();
                                dRoles.Note = row["Note"].ToString();
                                dRoles.Order = (int)row["Order"];
                                dRoles.CreatedTime = DateTime.Now;

                                dRoles.Insert();
                                index++;
                            }
                        }

                        //THEM THONG TIN VE CAC DANH HIEU CUA PHONG BAN
                        DataTable dtHName = (DataTable)ViewState["dtHName"];
                        if (dtHName.Rows[0]["Id"].ToString() != "0")
                        {
                            int index1 = 0;
                            cls_Department_HonourableName dHName;

                            while (index1 < dtHName.Rows.Count)
                            {
                                DataRow row = dtHName.Rows[index1];
                                dHName = new cls_Department_HonourableName();
                                dHName.DepartmentId = result;
                                dHName.Content = row["Content"].ToString();
                                dHName.CreatedTime = DateTime.Now;

                                dHName.Insert();
                                index1++;
                            }
                        }

                        ViewState["countDRoles"] = "0"; ViewState["dtDRoles"] = null;
                        ViewState["countHName"] = 0; ViewState["dtHName"] = null;

                        Label13.Text = "Cập nhật thông tin";
                        ddlCompany.SelectedValue = dInfor.CompanyId.ToString();
                        btnDelete.Visible = true;
                    }

                    if (result > 0)
                    {
                        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                               "MyScript", "alert('Cập nhật thông tin Phòng Ban " + txtDepartName.Text + " thành công.');", true);
                        Response.Redirect("Department.aspx?C_Id="+ddlBelongCompany.SelectedValue);
                        //bindTreeView();
                    }
                    else if (result == 0)
                         ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                               "MyScript", "alert('Không cập nhật được thông tin Phòng Ban " + txtDepartName.Text + ".');", true);
                    else
                        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                              "MyScript", "alert('Trùng tên Phòng Ban " + txtDepartName.Text + ", không thể cập nhật.');", true);
                }

            }
            catch (Exception ex)
            {
                ltlAnnouncement.Text = "Có lỗi trong quá trình cập nhật thông tin Phòng Ban " + txtDepartName.Text + ": " + ex.ToString();
            }
        }