protected void btnOK_Click(object sender, EventArgs e)
 {
     if (Page.IsValid)
     {
         try
         {
             CategoryEntity oCat = new CategoryEntity();
             oCat.sTitle = txtTitle.Text;
             oCat.sDesc = txtDesc.Text;
             oCat.iOrder = Convert.ToByte(txtOrder.Text);
             oCat.iTopID = Convert.ToInt32(cboTop.SelectedValue);
             if (btnOK.CommandName == "Edit")
             {
                 oCat.iCategoryID = Convert.ToInt32(btnOK.CommandArgument);
                 CategoryBRL.Edit(oCat);
             }
             else
                 CategoryBRL.Add(oCat);
             lblThongbao.Text = "Cập nhật thành công";
             //Nạp lại dữ liệu
             Response.Redirect(Request.Url.ToString());
             MultiView1.SetActiveView(View1);
         }
         catch (Exception ex)
         {
             Response.Write("<script language=\"javascript\">alert('" + ex.Message + "');location='Default.aspx?page=CategoryEdit';</script>");
         }
     }
 }
 /// <summary>
 /// Kiểm tra và chỉnh sửa Category
 /// </summary>
 /// <param name="entity">CategoryEntity</param>
 /// <returns>bool:kết quả thực hiện</returns>
 public static bool Edit(CategoryEntity entity)
 {
     checkExist(entity);
     checkLogic(entity);
     checkDuplicate(entity, false);
     return CategoryDAL.Edit(entity);
 }
 protected void lbtnDelete_Click(object sender, EventArgs e)
 {
     try
     {
         foreach (TableRow tr in Table1.Rows)
         {
             CheckBox chkDelete = tr.FindControl("chkDelete" + tr.ID) as CheckBox;
             if (chkDelete != null && chkDelete.Checked)
             {
                 int categoryID = Convert.ToInt32(tr.ID);
                 CategoryEntity obj = new CategoryEntity();
                 obj.iCategoryID = categoryID;
                 CategoryBRL.Remove(obj);
             }
         }
         //Nap lai du lieu
         Response.Redirect(Request.Url.ToString());
     }
     catch (Exception ex)
     { lblThongbao.Text = ex.Message; }
 }
 /// <summary>
 /// Kiểm tra và thêm mới Category
 /// </summary>
 /// <param name="entity">Entity</param>
 /// <returns>Int32: ID của Khách Hàng Mới Thêm Vào</returns>
 public static Int32 Add(CategoryEntity entity)
 {
     checkLogic(entity);
     checkDuplicate(entity, true);
     return CategoryDAL.Add(entity);
 }
 /// <summary>
 /// Kiểm tra logic Entity:xâu rỗng,null,số nhỏ hơn 0,....
 /// </summary>
 /// <param name="entity">CategoryEntity: Entity</param>
 private static void checkLogic(CategoryEntity entity)
 {
     if (String.IsNullOrEmpty(entity.sTitle))
         throw new Exception(EX_TITLE_EMPTY);
     if (entity.iOrder < 0)
         throw new Exception(EX_ORDER_INVALID);
     if (entity.iTopID < 0)
         throw new Exception(EX_TOPID_INVALID);
     if (entity.iCategoryID == entity.iTopID)
         throw new Exception(EX_TOPID_INVALID);
 }
 /// <summary>
 /// Kiểm tra tồn tại khóa ngoại
 /// </summary>
 /// <param name="entity">CategoryEntity:entity</param>
 private static void checkFK(CategoryEntity entity)
 {
     //Kiểm tra ràng buộc TopID
     CategoryEntity topEntity = CategoryDAL.GetOne(entity.iTopID);
     if (topEntity == null)
         throw new Exception(EX_TOPID_NOTEXIST);
 }
 private static void checkExist(CategoryEntity entity)
 {
     CategoryEntity topEntity = CategoryDAL.GetOne(entity.iCategoryID);
     if (topEntity == null)
         throw new Exception(EX_NOT_EXIST);
 }
 /// <summary>
 /// Kiểm tra trùng lặp bản ghi
 /// </summary>
 /// <param name="entity">CategoryEntity: CategoryEntity</param>
 /// <param name="CheckInsert">bool:Kiểm tra xem kiểm tra trong Insert hay Update</param>
 private static void checkDuplicate(CategoryEntity entity, bool CheckInsert)
 {
     //Kiểm tra xem tiêu đề đã tồn tại trong CSDL chưa
     List<CategoryEntity> list = CategoryDAL.GetAll();
     if (list.Exists(
         delegate(CategoryEntity oldEntity)
         {
             bool result=oldEntity.sTitle.Equals(entity.sTitle, StringComparison.OrdinalIgnoreCase);
             //Nếu kiểm tra cho Update thì phải kiểm tra khác ID
             if (CheckInsert == false)
                 result = result && oldEntity.iCategoryID != entity.iCategoryID;
             return result;
         }
     ))
     {
         list.Clear();
         throw new Exception(EX_TITLE_EXISTED);
     }
 }
 /// <summary>
 /// Kiểm tra ràng buộc khóa ngoại khi xóa
 /// </summary>
 /// <param name="entity">CategoryEntity : entity</param>
 private static void checkConstraint(CategoryEntity entity)
 {
     List<NewsCategoryEntity> lstNewsCat = NewsCategoryDAL.GetByiCategoryID(entity.iCategoryID);
     int count = lstNewsCat.Count;
     lstNewsCat.Clear();
     if (count > 0)
         throw new Exception(EX_NEWSCAT_CONSTRAINT);
 }
 /// <summary>
 /// Kiểm tra và xoá Category
 /// </summary>
 /// <param name="entity">CategoryEntity</param>
 /// <returns>bool:kết quả thực hiện</returns>
 public static bool Remove(CategoryEntity entity)
 {
     checkExist(entity);
     checkConstraint(entity);
     return CategoryDAL.Remove(entity.iCategoryID);
 }
 protected void lbtnDelete_Click(object sender, EventArgs e)
 {
     if (Page.IsValid)
     {
         try
         {
             foreach (GridViewRow row in grvCategory.Rows)
             {
                 CheckBox chkDelete = row.FindControl("chkDelete") as CheckBox;
                 int categoryID = Convert.ToInt32(grvCategory.DataKeys[row.RowIndex].Values["iCategoryID"]);
                 if (chkDelete != null && chkDelete.Checked)
                 {
                     CategoryEntity oCat = new CategoryEntity();
                     oCat.iCategoryID = categoryID;
                     CategoryBRL.Remove(oCat);
                 }
             }
             //Nap lai du lieu
             Response.Redirect(Request.Url.ToString());
         }
         catch (Exception ex)
         {
             Response.Write("<script language=\"javascript\">alert('" + ex.Message + "');location='Default.aspx?page=CategoryManager';</script>");
         }
     }
 }