protected void dgFaculty_ItemCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e) { Faculty faculty = new Faculty(); if (e.CommandName == "AddFaculty") { TextBox txtTempFaculty = (TextBox)e.Item.Cells[0].FindControl("txtFacultyNew"); //Validation for faculty (No digit allowed and only up to 50 characters) Regex facRegex = new Regex(@"[a-zA-Z() ]{1,50}$"); Match checkFac = facRegex.Match(txtTempFaculty.Text); if (txtTempFaculty.Text.Trim() == "") { ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type='text/javascript'>swal('Error!', 'Please fill in the faculty name!', 'error')</script>'"); } else if (!checkFac.Success) { ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type='text/javascript'>swal('Error!', 'Faculty name can only consist characters or ().', 'error')</script>'"); } else { if (faculty.Add(txtTempFaculty.Text) == false) { ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type='text/javascript'>swal('Error!', 'This " + txtTempFaculty.Text + " faculty is existed!', 'error')</script>'"); } else { ClientScript.RegisterStartupScript(this.GetType(), "Success", "<script type='text/javascript'>swal({title: 'Added!', text: 'This " + txtTempFaculty.Text + " faculty is added successfully', type : 'success', confirmButtonText : 'OK'}, function (isConfirm) { if (isConfirm) { window.location.href = 'cpFaculty.aspx'; }});</script>'"); } } } else if (e.CommandName == "DeleteFaculty") { Label rowFacultyID = (Label)e.Item.Cells[0].FindControl("lblID"); if (faculty.DeleteFaculty(rowFacultyID.Text) == false) { ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type='text/javascript'>swal('Error!', 'Delete this faculty unsuccessfully', 'error')</script>'"); } else { ClientScript.RegisterStartupScript(this.GetType(), "Success", "<script type='text/javascript'>swal({title: 'Deleted!', text: 'Deleted this faculty successfully', type : 'success', confirmButtonText : 'OK'}, function (isConfirm) { if (isConfirm) { window.location.href = 'cpFaculty.aspx'; }});</script>'"); } } }