protected void btnOK_Click(object sender, EventArgs e)
    {
        try
            {
                if (btnOK.Text.CompareTo("Thêm") == 0)
                {
                    txtPerDesc.Text = "";
                    txtPerName.Text = "";
                    btnOK.Text = "Lưu";
                    btnOK.CommandName = "Add";
                    txtPerDesc.Enabled = true;
                    txtPerName.Enabled = true;

                }
                else
                {
                    PermissionEntity oPer = new PermissionEntity();
                    oPer.sName = txtPerName.Text;
                    oPer.sDesc = txtPerDesc.Text;

                    if (btnOK.CommandName.CompareTo("Edit") == 0)
                    {
                        oPer.iPermissionID = Convert.ToInt32(btnOK.CommandArgument);
                        PermissionBRL.Edit(oPer);
                        lblThongbao.Text = "Cập nhật thành công";
                    }
                    else
                        PermissionBRL.Add(oPer);

                    Response.Redirect(Request.Url.ToString());

                }
            }
            catch (Exception ex)
            {
                Response.Write("<script language=\"javascript\">alert('" + ex.Message + "');location='Default.aspx?page=Permission';</script>");
            }
    }
    protected void lbtnDelete_Click(object sender, EventArgs e)
    {
        try
            {

                foreach (GridViewRow row in grvPermission.Rows)
                {
                    CheckBox chkDelete = row.FindControl("chkDelete") as CheckBox;
                    int permissionID = Convert.ToInt32(grvPermission.DataKeys[row.RowIndex].Values["iPermissionID"]);
                    if (chkDelete != null && chkDelete.Checked)
                    {
                        PermissionEntity oPer = new PermissionEntity();
                        oPer.iPermissionID = permissionID;
                        PermissionBRL.Remove(oPer);
                    }
                }
                //Nap lai du lieu
                Response.Redirect(Request.Url.ToString());
            }
            catch (Exception ex)
            {
                Response.Write("<script language=\"javascript\">alert('" + ex.Message + "');location='Default.aspx?page=Permission';</script>");
            }
    }
 /// <summary>
 /// Kiểm tra tồn tại khóa ngoại
 /// </summary>
 /// <param name="entity">PermissionEntity:entity</param>
 private static void checkFK(PermissionEntity entity)
 {
     PermissionEntity oPermission = PermissionDAL.GetOne(entity.iPermissionID);
     //oPermission.sDesc
     //oPermission.sName
     if (oPermission == null)
     {
         throw new Exception(EX_PERMISSIONID_NOTFOUND);
     }
 }
 /// <summary>
 /// Kiểm tra logic Entity
 /// </summary>
 /// <param name="entity">PermissionEntity: entity</param>
 private static void checkLogic(PermissionEntity entity)
 {
     if (String.IsNullOrEmpty(entity.sName))
         throw new Exception(EX_SNAME_EMPTY);
 }
 private static void checkExist(PermissionEntity entity)
 {
     PermissionEntity oPermission=PermissionDAL.GetOne(entity.iPermissionID);
     if(oPermission==null)
         throw new Exception(EX_NOT_EXIST);
 }
 /// <summary>
 /// Kiểm tra trùng lặp bản ghi
 /// </summary>
 /// <param name="entity">PermissionEntity: PermissionEntity</param>
 private static void checkDuplicate(PermissionEntity entity,bool CheckInsert)
 {
     List<PermissionEntity> list = PermissionDAL.GetAll();
     if (list.Exists(
         delegate(PermissionEntity oldEntity)
         {
             bool result =oldEntity.sName.Equals(entity.sName, StringComparison.OrdinalIgnoreCase);
             if(!CheckInsert)
                 result=result && oldEntity.iPermissionID != entity.iPermissionID;
             return result;
         }
     ))
     {
         list.Clear();
         throw new Exception(EX_PERMISSION_EXISTED);
     }
 }
        /// <summary>
        /// Kiểm tra và xoá Permission
        /// </summary>
        /// <param name="entity">PermissionEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(PermissionEntity entity)
        {
            checkExist(entity);

            return PermissionDAL.Remove(entity.iPermissionID);
        }
 /// <summary>
 /// Kiểm tra và chỉnh sửa Permission
 /// </summary>
 /// <param name="entity">PermissionEntity</param>
 /// <returns>bool:kết quả thực hiện</returns>
 public static bool Edit(PermissionEntity entity)
 {
     checkExist(entity);
     checkLogic(entity);
     checkDuplicate(entity, false);
     checkFK(entity);
     return PermissionDAL.Edit(entity);
 }
 /// <summary>
 /// Kiểm tra và thêm mới Permission
 /// </summary>
 /// <param name="entity">Entity</param>
 /// <returns>Int32: ID của Permission Mới Thêm Vào</returns>
 public static Int32 Add(PermissionEntity entity)
 {
     checkLogic(entity);
     checkDuplicate(entity, true);
     //checkFK(entity);
     return PermissionDAL.Add(entity);
 }