protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                cls_DepartmentInformation dInfor = new cls_DepartmentInformation();
                dInfor.Id = Convert.ToInt32(Request["D_Id"]);
                int result = dInfor.Delete();

                ddlCompany.SelectedValue = ddlBelongCompany.SelectedValue;

                if (result > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                                   "MyScript", "alert('Xóa thông tin Phòng Ban thành công.');", true);
                    if (Request.UrlReferrer != null)
                        Response.Redirect(Request.UrlReferrer.ToString());
                    else
                        Response.Redirect("Department.aspx");
                }
                else
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                                   "MyScript", "alert('Không xóa được thông tin Phòng Ban. Vui lòng thử lại !!!');", true);
            }
            catch (Exception ex)
            {
                ltlAnnouncement.Text = "Có lỗi khi xóa thông tin Phòng Ban: " + ex.ToString();
            }
        }
 private void LoadDataToDepartment()
 {
     if (trDepartment.Nodes.Count > 0)
         trDepartment.Nodes.Clear();
     cls_DepartmentInformation objDe = new cls_DepartmentInformation();
     dtDepartment = objDe.GetList();
     ShowDSTreeview(trDepartment, dtDepartment, 0, "Id", "Name", "Belong_DepartmentId","Toàn bộ phòng ban");
     trDepartment.ExpandAllNodes();
 }
        private void bindTreeView(RadTreeView rtrDepartment)
        {
            // ---- BIND DU LIEU VAO LISTVIEW --
            cls_DepartmentInformation dInfor = new cls_DepartmentInformation();
            dInfor.CompanyId = 1;

              DataTable  dtDepart = dInfor.SelectForSalary();
            ViewState["DT_DEPARTMENT"] = dtDepart;

            //int countDepartment = dtDepart.Rows.Count;
            //ShowDSTreeview(0, rtrDepartment);
            RadTreeNode node = new RadTreeNode();
            node.Text = "--- Lựa chọn ---";
            node.Value = "0";

            rtrDepartment.Nodes.Add(node);
            addNodeTreeView("0", node, dtDepart);
        }
        private void bindTreeView(RadTreeView rtrDepartment)
        {
            // ---- BIND DU LIEU VAO LISTVIEW --
            cls_DepartmentInformation dInfor = new cls_DepartmentInformation();
            dInfor.CompanyId = Convert.ToInt16(ddlBelongCompany.SelectedValue);
            DataTable dtDepart = dInfor.SelectByComId();
            ViewState["DT_DEPARTMENT"] = dtDepart;

            RadTreeNode node = new RadTreeNode();
            node.Text = "--- Lựa chọn ---";
            node.Value = "0";

            rtrDepartment.Nodes.Add(node);
            addNodeTreeView("0", node, dtDepart);
        }
        private void bindListDepartment()
        {
            try
            {
                cls_DepartmentInformation dInfor = new cls_DepartmentInformation();
                dInfor.CompanyId = Convert.ToInt16(ddlCompany.SelectedValue);

                DataTable dtDInfo = dInfor.SelectDTS();
                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < dtDInfo.Rows.Count; i++)
                {
                    sb.Append("<tr>"+
                              "  <td nowrap=\"nowrap\" align=\"center\">"+
                              "          "+(i+1).ToString()+
                              "      </td><td nowrap=\"nowrap\" align=\"Left\">" + dtDInfo.Rows[i]["ExtendStr"].ToString() +
                              "          <a class=\"NormalBold\" href=\"Department.aspx?D_Id=" + dtDInfo.Rows[i]["Id"].ToString() + "&D_Action=1\">" +
                              "              <acronym title=''>" + dtDInfo.Rows[i]["DepartNameNature"].ToString() +
                              "              </acronym>"+
                              "          </a>"+
                              "      </td><td valign=\"Top\">" + dtDInfo.Rows[i]["Established_Number"].ToString() +
                              "      </td><td align=\"Center\" valign=\"Top\">" + dtDInfo.Rows[i]["Established_Date"].ToString() +
                              "      </td><td valign=\"Top\">" + dtDInfo.Rows[i]["Established_Signer"].ToString() +
                              "      </td><td valign=\"Top\">" + dtDInfo.Rows[i]["LeaderMember"].ToString() +
                              "      </td>"+
                            "</tr>");
                }

                ltl_ListDepartment.Text = sb.ToString();
            }
            catch (Exception ex)
            {
            }
        }
        /*
        private void bindTreeView()
        {
            trDepartment.Nodes.Clear();
            // ---- BIND DU LIEU VAO LISTVIEW --
            cls_DepartmentInformation dInfor = new cls_DepartmentInformation();
            dInfor.CompanyId = Convert.ToInt16(ddlCompany.SelectedValue);
            DataTable dtDepart = dInfor.SelectByComId();
            ViewState["countDepartment"] = dtDepart.Rows.Count.ToString();

            DataRow[] _Row = dtDepart.Select("DepartParentId=0");
            if (_Row.Count() > 0)
            {
                for (int i = 0; i < _Row.Count(); i++)
                {
                    TreeNode node = new TreeNode();
                    node.Text = _Row[i]["DepartName"].ToString();
                    node.Value = _Row[i]["Id"].ToString();

                    trDepartment.Nodes.Add(node);
                    addNode(_Row[i]["Id"].ToString(), node, dtDepart);
                }
            }
        }

        private void addNode(string parentId, TreeNode parentNode, DataTable dtDepart)
        {
            try
            {
                DataRow[] _Row = dtDepart.Select("DepartParentId=" + parentId);
                if (_Row.Count() > 0)
                {
                    for (int i = 0; i < _Row.Count(); i++)
                    {
                        TreeNode childNode = new TreeNode();
                        childNode.Text = _Row[i]["DepartName"].ToString();
                        childNode.Value = _Row[i]["Id"].ToString();

                        parentNode.ChildNodes.Add(childNode);
                        addNode(_Row[i]["Id"].ToString(), childNode, dtDepart);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
         * */
        private void bindData()
        {
            ckbActive.Checked = true;

            if (dAction != 2)
            {
                try
                {
                    //LAY THONG TIN PHONG BAN
                    cls_DepartmentInformation cDepart = new cls_DepartmentInformation();
                    cDepart.Id = Convert.ToInt32(Request["D_Id"]);
                    cDepart = cDepart.Select();

                    if (cDepart != null)
                    {
                        txtDepartName.Text = cDepart.Name;
                        rtxtOrder.Text = cDepart.Order.ToString();
                        ddlBelongCompany.SelectedValue = cDepart.CompanyId.ToString();
                        if(cDepart.Established_Date.CompareTo(new DateTime(1900, 1, 1)) > 0)
                            dateEstablish.SelectedDate = cDepart.Established_Date;

                        txtEstaNumber.Text = cDepart.Established_Number;
                        txtSigner.Text = cDepart.Established_Signer;
                        txtSeatPosition.Text = cDepart.SeatPosition;
                        ckbActive.Checked = (cDepart.IsActive == 1) ? true : false;
                        ckbCalSalary.Checked = (cDepart.CalculateSalary == 1) ? true : false;

                        if (cDepart.CompanyId > 0)
                        {
                            rcbDepartment_Bind();
                            bindRadCombo();
                        }

                        RadTreeView rTree = (RadTreeView)rcbDepartment.Items[0].FindControl("rtrDepartment");
                        RadTreeNode node = rTree.FindNodeByValue(cDepart.Belong_DepartmentId.ToString());
                        node.Selected = true;
                        rcbDepartment.Items[0].Text = node.Text;

                        cboLeader.SelectedValue = cDepart.LeaderId.ToString();
                        cboAdministrator.SelectedValue = cDepart.AdministratorId.ToString();
                    }
                    bind_M_Grid_Data(-1);
                    bind_Ls_HonourName(-1);
                }
                catch (Exception ex)
                {
                }
            }
            else                                    //insert them department moi
            {
                bind_M_Grid_Data(-1);
                bind_Ls_HonourName(-1);

                if (ViewState["countHName"].ToString() == "0")
                    lvHonourName.Items[0].Visible = false;
                if (ViewState["countDRoles"].ToString() == "0")
                    m_grid.Rows[0].Visible = false;

                if ((ViewState["countDRoles"].ToString() == "0") && (ViewState["countHName"].ToString() == "0"))
                {
                    //VIEW DU LIEU LEN
                    txtDepartName.Text = "";
                    rtxtOrder.Text = "";
                    txtEstaNumber.Text = "";
                    txtSigner.Text = "";
                }
                rcbDepartment_Bind();
                bindRadCombo();

                RadTreeView rTree = (RadTreeView)rcbDepartment.Items[0].FindControl("rtrDepartment");
                rTree.Nodes[0].Selected = true;
                rcbDepartment.Items[0].Text = rTree.Nodes[0].Text;
                cboAdministrator.SelectedValue = cboLeader.SelectedValue = "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();
            }
        }
        private void bindRepeaterG(int gId)
        {
            try
            {
                //LAY DANH SACH CONG TY
                cls_Module_Authority_Employee aEmp = new cls_Module_Authority_Employee();
                aEmp.EmployeeId = Convert.ToInt32(Session["UserId"]);

                DataTable dt = aEmp.GetCompanyByAuthorityCode(ConfigurationSettings.AppSettings["PermissionAuthority"]);
                ddlCompanySelect1.DataSource = dt;
                ddlCompanySelect1.DataBind();
                ViewState["DT_COMPANY"] = dt;
                ViewState["DT_DEPARTMENT"] = new cls_DepartmentInformation().SelectList(0);

                bindGroupList(gId);

                short moduleId;
                isChange = true;
                dt = new cls_Module_Authority_Group().SelectByGId(Convert.ToInt32(Session["UserId"]), ConfigurationSettings.AppSettings["Emp_Module"],
                                                            ConfigurationSettings.AppSettings["PermissionAuthority"], gId, out moduleId);

                List<cls_Module_Authority_Employee> list = getList(dt, moduleId);
                ViewState["DT_GAUTHORITY"] = dt;

                rptData1.DataSource = list;
                rptData1.DataBind();
            }
            catch (Exception ex)
            {
            }
        }
        private void bindRepeaterE(int empId)
        {
            try
            {
                //LAY DANH SACH CONG TY
                cls_Module_Authority_Employee aEmp = new cls_Module_Authority_Employee();
                aEmp.EmployeeId = Convert.ToInt32(Session["UserId"]);

                DataTable dt = aEmp.GetCompanyByAuthorityCode(ConfigurationSettings.AppSettings["PermissionAuthority"]);
                ddlCompanySelect.DataSource = dt;
                ddlCompanySelect.DataBind();
                ViewState["DT_COMPANY"] = dt;
                ViewState["DT_DEPARTMENT"] = new cls_DepartmentInformation().SelectList(0);

                bindCboEmployee(empId);

                short moduleId, isAdmin;
                dt = aEmp.SelectByEId(ConfigurationSettings.AppSettings["Emp_Module"], ConfigurationSettings.AppSettings["PermissionAuthority"],
                                        empId, out moduleId, out isAdmin);

                if (isAdmin > 0)
                    btnESave.Enabled = isChange = false;
                else
                    btnESave.Enabled = isChange = true;

                List<cls_Module_Authority_Employee> list = getList(dt, moduleId);
                ViewState["DT_EAUTHORITY"] = dt;

                rptData.DataSource = list;
                rptData.DataBind();
            }
            catch (Exception ex)
            {
            }
        }