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)
            {
            }
        }
        protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            int gId = Convert.ToInt32(ddlGroup.SelectedValue);

            if (gId > 0)
            {
                short moduleId;
                isChange = true;
                DataTable 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();
            }
        }
        private void bindPermissionList()
        {
            try
            {
                // --- BIND CAC NHOM DA DUOC PHAN QUYEN ---
                // --- LAY DANH SACH CAC QUYEN CUA NGUOI DUNG HIEN TAI DUOC PHAN ---

                cls_Module_Authority_Employee aEmp = new cls_Module_Authority_Employee();
                aEmp.EmployeeId = Convert.ToInt32(Session["UserId"]);
                DataTable dtAuthority = aEmp.GetByModuleCode(ConfigurationSettings.AppSettings["Emp_Module"]);

                StringBuilder sb = new StringBuilder();
                sb.Append(" <table class=\"basic-table\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">" +
                             "  <tbody><tr>" +
                             "      <td>" +
                             "          <table class=\"form-table\" border=\"0\" cellpadding=\"4\"><tbody>" +
                             "              <tr class=\"gray\">" +
                             "                  <td colspan=\"" + dtAuthority.Rows.Count.ToString() + "\">PHÂN QUYỀN NHÓM NGƯỜI DÙNG</td>" +
                             "                  <td align=\"right\" colspan=\"2\"><a href=\"Permission.aspx?P_action=2\">+Bổ sung</a></td>" +
                             "              </tr>" +
                             "              <tr bgcolor=\"whitesmoke\">" +
                             "                  <td class=\"Normal\" width=\"180px\"> <u>Nhóm đã phân quyền </u></td>");
                for (int i = 0; i < dtAuthority.Rows.Count; i++)
                {
                    sb.Append("                  <td class=\"NormalBold\" align=\"center\">Quyền " + dtAuthority.Rows[i]["AuthorityName"].ToString() + "</td>");
                }
                sb.Append("                  <td class=\"normal\">Cập nhật</td>" +
                          "              </tr>");

                cls_Module_Authority_Group aGroup = new cls_Module_Authority_Group();
                aGroup.CompanyId = Convert.ToInt16(ddlCompany.SelectedValue);
                DataTable dtGroup = aGroup.SelectGroupByCompany(ConfigurationSettings.AppSettings["Emp_Module"]);

                if (dtGroup.Rows.Count > 0)
                {
                    DataTable dtGAuthority = aGroup.SelectByCompany(ConfigurationSettings.AppSettings["Emp_Module"], Convert.ToInt32(Session["UserId"]));

                    for (int i = 0; i < dtGroup.Rows.Count; i++)
                    {
                        DataRow[] _Row = dtGAuthority.Select("GroupId=" + dtGroup.Rows[i]["GroupId"].ToString(), "AuthorityCode asc");

                        sb.Append("<tr>" +
                                  "     <td class=\"NormalBold\"><img src=\"../images/Message/user-group.png\" /> " + _Row[0]["GroupName"].ToString() + "</td>");

                        for (int j = 0; j < _Row.Count(); j++)
                        {
                            if (_Row[j]["IsUse"].ToString() == "1")
                                sb.Append("     <Td class=\"normal\" align=\"center\"><img src=\"../images/Update.gif\" align=\"absmiddle\"> </td>");
                            else
                                sb.Append("     <Td class=\"normal\" align=\"center\">&nbsp;</td>");
                        }

                        sb.Append("     <td class=\"normal\" align=\"center\"><a href=\"Permission.aspx?P_action=2&G_Id=" + dtGroup.Rows[i]["GroupId"].ToString() + "\">Sửa</a></td>" +
                                  "</tr>");
                    }
                }
                else
                {
                    sb.Append("<tr><td class=\"NormalBold\" align=\"center\" colspan=\"" + (dtAuthority.Rows.Count + 2).ToString() + "\"><i>Hiện chưa có nhóm nào được phân quyền.</i></td></tr>");
                }
                sb.Append("              </tbody>" +
                          "          </table>" +
                          "      </td>" +
                          "  </tr>" +
                        "</tbody></table>");
                ltlListGroup.Text = sb.ToString();

                // --- BIND CAC NGUOI DUNG DA DUOC PHAN QUYEN ---
                sb = new StringBuilder();
                sb.Append(" <table class=\"basic-table\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">" +
                             "  <tbody><tr>" +
                             "      <td>" +
                             "          <table class=\"form-table\" border=\"0\" cellpadding=\"4\"><tbody>" +
                             "              <tr class=\"gray\">" +
                             "                  <td colspan=\"" + dtAuthority.Rows.Count.ToString() + "\">PHÂN QUYỀN NGƯỜI DÙNG</td>" +
                             "                  <td align=\"right\" colspan=\"2\"><a href=\"Permission.aspx?P_action=3\">+Bổ sung</a></td>" +
                             "              </tr>" +
                             "              <tr bgcolor=\"whitesmoke\">" +
                             "                  <td class=\"Normal\" width=\"180px\"><u>Người dùng đã phân quyền </u></td>");
                for (int i = 0; i < dtAuthority.Rows.Count; i++)
                {
                    sb.Append("                  <td class=\"NormalBold\" align=\"center\">Quyền " + dtAuthority.Rows[i]["AuthorityName"].ToString() + "</td>");
                }
                sb.Append("                  <td class=\"normal\">Cập nhật</td>" +
                          "              </tr>");

                aEmp = new cls_Module_Authority_Employee();
                aEmp.EmployeeId = Convert.ToInt32(Session["UserId"]);
                aEmp.CompanyId = Convert.ToInt16(ddlCompany.SelectedValue);

                DataTable dtEmp = aEmp.SelectEmpByCompany(ConfigurationSettings.AppSettings["Emp_Module"]);

                if (dtEmp.Rows.Count > 0)
                {
                    DataTable dtEAuthority = aEmp.SelectByCompany(ConfigurationSettings.AppSettings["Emp_Module"]);

                    for (int i = 0; i < dtEmp.Rows.Count; i++)
                    {
                        DataRow[] _Row = dtEAuthority.Select("EmployeeId=" + dtEmp.Rows[i]["EmployeeId"].ToString(), "AuthorityCode asc");

                        sb.Append("<tr>" +
                                  "     <td class=\"NormalBold\"><img src=\"../images/MRoom/user.gif\" /> " + _Row[0]["EmployName"].ToString() + "</td>");

                        for (int j = 0; j < _Row.Count(); j++)
                        {
                            if (_Row[j]["IsUse"].ToString() == "1")
                                sb.Append("     <Td class=\"normal\" align=\"center\"><img src=\"../images/Update.gif\" align=\"absmiddle\"> </td>");
                            else
                                sb.Append("     <Td class=\"normal\" align=\"center\">&nbsp;</td>");
                        }

                        sb.Append("     <td class=\"normal\" align=\"center\"><a href=\"Permission.aspx?P_action=3&E_Id=" + dtEmp.Rows[i]["EmployeeId"].ToString() + "\">Sửa</a></td>" +
                                  "</tr>");
                    }
                }
                else
                {
                    sb.Append("<tr><td class=\"NormalBold\" align=\"center\" colspan=\"" + (dtAuthority.Rows.Count + 2).ToString() + "\"><i>Hiện chưa có người dùng nào được phân quyền.</i></td></tr>");
                }
                sb.Append("              </tbody>" +
                          "          </table>" +
                          "      </td>" +
                          "  </tr>" +
                        "</tbody></table>");
                ltlListEmployee.Text = sb.ToString();

            }
            catch (Exception ex)
            {
            }
        }
        protected void btnGSave_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dtCompany = (DataTable)ViewState["DT_COMPANY"];
                DataTable dtDepartment = (DataTable)ViewState["DT_DEPARTMENT"];
                DataTable dtOld = (DataTable)ViewState["DT_GAUTHORITY"];

                DataTable dtGAuthority = new DataTable();
                dtGAuthority.Columns.Add("Id", typeof(int));
                dtGAuthority.Columns.Add("ModuleId");
                dtGAuthority.Columns.Add("AuthorityId");
                dtGAuthority.Columns.Add("GroupId");
                dtGAuthority.Columns.Add("CompanyId");
                dtGAuthority.Columns.Add("DepartmentId");
                dtGAuthority.Columns.Add("CreatedTime");

                int g_id;
                short mId = 0;
                g_id = Convert.ToInt32(ddlGroup.SelectedValue);

                if (g_id == 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                                    "MyScript", "alert('Bạn phải chọn Nhóm người dùng cần phân quyền trước !');", true);
                    ddlGroup.Focus();
                    return;
                }

                for (int i = 0; i < rptData1.Items.Count; i++)
                {
                    RepeaterItem item = rptData1.Items[i];

                    var chkSelectA = (CheckBox)item.FindControl("chkSelectA");
                    var lblAuthorityId = (Label)item.FindControl("lblAuthorityId");
                    var lblModuleId = (Label)item.FindControl("lblModuleId");
                    mId = Convert.ToInt16(lblModuleId.Text);
                    var lbAuthorityName = (Label)item.FindControl("lbAuthorityName");

                    var txtComName = (TextBox)item.FindControl("txtComName");
                    var txtDepartName = (TextBox)item.FindControl("txtDepartName");

                    if (chkSelectA.Checked == true)
                    {
                        if (txtComName.Text.Length <= 0)
                        {
                            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                                    "MyScript", "alert('Bạn phải chọn Công ty cho Quyền \"" + lbAuthorityName.Text + "\" !');", true);
                            txtComName.Focus();
                            return;
                        }
                        if (txtDepartName.Visible == true && txtDepartName.Text.Length <= 0)
                        {
                            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                                    "MyScript", "alert('Bạn phải chọn Phòng/Ban cho Quyền \"" + lbAuthorityName.Text + "\" !');", true);
                            txtDepartName.Focus();
                            return;
                        }

                        if (txtDepartName.Visible == true)
                        {
                            DataTable dtDepartmentId = getDepartmentIdList(txtDepartName.Text, dtDepartment);

                            for (int j = 0; j < dtDepartmentId.Rows.Count; j++)
                            {
                                DataRow dRow = dtGAuthority.NewRow();
                                DataRow[] _Row = dtOld.Select("AuthorityId=" + lblAuthorityId.Text + " AND DepartmentId=" + dtDepartmentId.Rows[j]["Id"].ToString());

                                if (_Row.Count() > 0)
                                    dRow["Id"] = _Row[0]["Id"];
                                else
                                    dRow["Id"] = "0";

                                dRow["ModuleId"] = lblModuleId.Text;
                                dRow["AuthorityId"] = lblAuthorityId.Text;
                                dRow["GroupId"] = g_id;
                                dRow["CompanyId"] = dtDepartmentId.Rows[j]["CompanyId"];
                                dRow["DepartmentId"] = dtDepartmentId.Rows[j]["Id"];
                                dRow["CreatedTime"] = DateTime.Now;

                                dtGAuthority.Rows.Add(dRow);
                            }
                        }
                        else
                        {
                            DataTable dtCompanyId = getCompanyIdList(txtComName.Text, dtCompany);

                            for (int j = 0; j < dtCompanyId.Rows.Count; j++)
                            {
                                DataRow dRow = dtGAuthority.NewRow();
                                DataRow[] _Row = dtOld.Select("AuthorityId=" + lblAuthorityId.Text + " AND CompanyId=" + dtCompanyId.Rows[j]["Id"].ToString());

                                if (_Row.Count() > 0)
                                    dRow["Id"] = _Row[0]["Id"];
                                else
                                    dRow["Id"] = "0";

                                dRow["ModuleId"] = lblModuleId.Text;
                                dRow["AuthorityId"] = lblAuthorityId.Text;
                                dRow["GroupId"] = g_id;
                                dRow["CompanyId"] = dtCompanyId.Rows[j]["Id"];
                                dRow["DepartmentId"] = 0;
                                dRow["CreatedTime"] = DateTime.Now;

                                dtGAuthority.Rows.Add(dRow);
                            }
                        }
                    }
                }
                int resultUpdate = new cls_Module_Authority_Group().ExecuteUpdateById(dtGAuthority, g_id, mId);

                if (resultUpdate > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                        "MyScript", "alert('Cập nhật phân quyền thao tác cho Nhóm người dùng thành công!');", true);
                    Response.Redirect("Permission.aspx");
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                        "MyScript", "alert('Có lỗi trong quá trình cập nhật, vui lòng thử lại !!!');", true);
                }
            }
            catch (Exception ex)
            {
            }
        }