CẤU TRÚC CHƯƠNG TRÌNH ĐƯỢC ÁP DỤNG MÔ HÌNH MVC
Chương trình được cấu trúc gồm 3 phần,đúng với cấu trúc của mô hình
MVC,gồm các phần như sau:
I.View(Interface_Hiển thị):
1.Truy cập vào web:
*Login:
-phần này người quản lý hệ thống dùng để đăng nhập vào chương trình:
*Giao diện:
*code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Interface_AccessWebsite_login : System.Web.UI.Page
{
private AccountDB objAccountDB = new AccountDB();
protected void Page_Load(object sender, EventArgs e)
{
}
public string GetApplicationPath()
{
string applicationPath = "";
if (this.Page.Request.Url != null)
applicationPath = this.Page.Request.Url.AbsoluteUri.Substring(
0, this.Request.Url.AbsoluteUri.ToLower().IndexOf(
this.Request.ApplicationPath.ToLower(),
this.Request.Url.AbsoluteUri.ToLower().IndexOf(
this.Page.Request.Url.Authority.ToLower()) +
this.Page.Request.Url.Authority.Length) +
this.Request.ApplicationPath.Length);
return applicationPath;
}
protected void btnLogin_Click(object sender, EventArgs e)
{
string strUsername = this.txtUsername.Text;
string strPassword = this.txtPassword.Text;
AccountDetails objAccountDetails = new AccountDetails(strUsername,
strPassword);
int intCount = objAccountDB.CheckAccount(objAccountDetails);
Session["sesUsername"] = strUsername;
Session["sesPassword"] = strPassword;
if (intCount == -1)
{
Response.Write(objAccountDB.ErrorDetail);
Response.Write("<br>");
return;
}
if (intCount != 0)
{
Response.Redirect(GetApplicationPath() +
"/Interface/home.aspx?page=interface/wellcome.aspx");
}
Response.Write("don't access right!");
}
}
a.Danh sách tài khoản:
*Giao diện:
Code:
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Interface_AccessWebsite_AccountList : System.Web.UI.Page
{
private AccountDB objAccountDB = new AccountDB();
private AccountDetails[] arrAccountDetails;
protected void Page_Load(object sender, EventArgs e)
{
arrAccountDetails = objAccountDB.GetAllAccounts();
this.grdAccounts.DataSource = arrAccountDetails;
grdAccounts.DataBind();
}
protected string GetApplicationPath()
{
string applicationPath = "";
if (this.Page.Request.Url != null)
applicationPath = this.Page.Request.Url.AbsoluteUri.Substring(
0, this.Request.Url.AbsoluteUri.ToLower().IndexOf(
this.Request.ApplicationPath.ToLower(),
this.Request.Url.AbsoluteUri.ToLower().IndexOf(
this.Page.Request.Url.Authority.ToLower()) +
this.Page.Request.Url.Authority.Length) +
this.Request.ApplicationPath.Length);
return applicationPath;
}
protected void grdAccounts_SelectedIndexChanged(object sender, EventArgs
e)
{
}
}
b.Thay đổi Mật mã:
*Giao diện:
*Code:
public partial class Interface_AccessWebsite_ChangPassword :
System.Web.UI.Page
{
private AccountDB objAccountDB = new AccountDB();
private AccountDetails objAccountDetails = new AccountDetails();
protected void Page_Load(object sender, EventArgs e)
{
}
public string GetApplicationPath()
{
string applicationPath = "";
if (this.Page.Request.Url != null)
applicationPath = this.Page.Request.Url.AbsoluteUri.Substring(
0, this.Request.Url.AbsoluteUri.ToLower().IndexOf(
this.Request.ApplicationPath.ToLower(),
this.Request.Url.AbsoluteUri.ToLower().IndexOf(
this.Page.Request.Url.Authority.ToLower()) +
this.Page.Request.Url.Authority.Length) +
this.Request.ApplicationPath.Length);
return applicationPath;
}
protected void btnChange_Click(object sender, EventArgs e)
{
// Check suitable between Password and ConfirmPassword
if (this.txtConfirmPassword.Text != this.txtPassword.Text)
{
this.lblMessage.Text = "Password anh confirm is suitable!";
return;
}
// Change Password
objAccountDetails.Username = (string)Session["sesUsername"];
objAccountDetails.Password = this.txtPassword.Text;
int intError = objAccountDB.ChangePassword(objAccountDetails);
// check blank pass
//if (objAccountDB.ChangePassword(objAccountDetails) == 1 )
//{
// check error
if (intError == 0)
{
this.lblMessage.Text = "Don't use blank password";
return;
}
else
{
this.lblMessage.Text = "Password chage successfully!";
return;
}
}
}
c.Tạo người dùng mới:
*Giao diện:
*Code:
public partial class Interface_AccessWebsite_CreateUser : System.Web.UI.Page
{
private AccountDB accdb = new AccountDB();
private AccountDetails accdetail = new AccountDetails();
private Common objCommon = new Common();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Accept_Click(object sender, EventArgs e)
{
string _username = this.txtUsername.Text;
string _password = this.txtPassword.Text;
if (objCommon.CheckEmpty(this.txtUsername.Text) == 1)
{
Response.Write("Don't use blank Username !");
return;
}
if (objCommon.CheckEmpty(this.txtPassword.Text) == 1)
{
Response.Write ("Don't use blank password !");
return;
}
if (this.txtPassword.Text != this.txtConf.Text)
{
Response.Write("Password anh confirm is suitable!");
return;
}
string _administrator;
if (CheckBox1.Checked)
_administrator = "1";
else
_administrator = "0";
AccountDetails accdetail = new AccountDetails(_username,
_password, _administrator);
int _count = accdb.CheckAccount(_username);
if (_count == 0)
{
Response.Write("Account is not readly!");
int _flag = accdb.InsertAccount(accdetail);
if (_flag == 1)
{
Response.Write("Insert Account is success!");
}
else
{
Response.Write("Insert Account is not success!");
}
}
else
{
Response.Write("Account is readly!");
}
}
}
d.Xóa tài khoản:
*Giao diện:
*Code:
public partial class Interface_AccessWebsite_DeleteAcount : System.Web.UI.Page
{
private AccountDB objAccountDB = new AccountDB();
protected void Page_Load(object sender, EventArgs e)
{
DialogResult reply;
reply = MessageBox.Show("Are you sure ?", "Yes or No Demo",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (reply == DialogResult.Yes)
{
int intError =
objAccountDB.DeleteAccount(Request.QueryString["username"]);
if (intError == 1)
{
Response.Redirect("~/interface/accesswebsite/AccountList.aspx");
return;
}
Response.Write(objAccountDB.ErrorDetail);
return;
}
else
Response.Redirect("~/interface/accesswebsite/AccountList.aspx");
}
e.Chỉnh sửa tài khoản:
public partial class Interface_AccessWebsite_EditAccount : System.Web.UI.Page
{
private AccountDB objAccountDB = new AccountDB();
private AccountDetails objAccountDetails = new AccountDetails();
private Common objCommon = new Common();
protected void Page_Load(object sender, EventArgs e)
{
objAccountDetails =
objAccountDB.GetAccount(Request.QueryString["Username"]);
this.txtUsername.Text = objAccountDetails.Username;
if (IsPostBack)
{
if (objAccountDetails.Administrator == "1")
{
chkAdministrator.Checked = true;
}
else
{
chkAdministrator.Checked = false;
}
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
}
protected void btOK_Click(object sender, EventArgs e)
{
string _username = this.txtUsername.Text;
string _password = this.txtPassword.Text;
if (objCommon.CheckEmpty(this.txtPassword.Text) == 1)
{
Response.Write("Dont use Blank Username !!");
}
if (this.txtPassword.Text != this.txtConfirmPassword.Text)
{
Response.Write("Password anh confirm is suitable");
}
string _administrator;
if (chkAdministrator.Checked)
{
_administrator = "1";
}
else
{
_administrator = "0";
}
AccountDetails accdetail = new AccountDetails(_username, _password,
_administrator);
int _count = objAccountDB.UpdateAccount(accdetail);
if (_count == 0)
{
Response.Write("Username do not match !");
}
else
{
int _flag = objAccountDB.UpdateAccount(accdetail);
if (_flag == 1)
{
Response.Write("Update success");
return;
}
else
{
Response.Write("Update Account is not success!");
}
}
}
protected void chkAdministrator_CheckedChanged(object sender, EventArgs e)
{
}
}
2.Nhân viên:
a.Xóa nhân viên:
*Giao diện:
*Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Interface_Employees_DeleteEmp : System.Web.UI.Page
{
private EmployeeDB objEmployeeDB = new EmployeeDB();
protected void Page_Load(object sender, EventArgs e)
{
int intError =
objEmployeeDB.DeleteEmployee(Request.QueryString["EmployeeID"]);
if (intError == 1)
{
Response.Redirect("~/interface/employees/emplist.aspx");
return;
}
Response.Write(objEmployeeDB.ErrorDetail);
}
}
b.Cập nhật thông tin nhân viên:
*Giao diện:
public partial class Interface_Employees_EditEmployee : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
EmployeeDB objEmployeeDB = new EmployeeDB();
EmployeeDetails objEmployeeDetails = new EmployeeDetails();
SetRangOfList(lstDayOfDateOfBirth, 1, 31);
SetRangOfList(lstDayOfEndDate, 1, 31);
SetRangOfList(lstDayOfIssuedDate, 1, 31);
SetRangOfList(lstDayOfProbEnd, 1, 31);
SetRangOfList(lstDayOfStartDate, 1, 31);
SetRangOfList(lstMonthOfDateOfBirth, 1, 12);
SetRangOfList(lstMonthOfEndDate, 1, 12);
SetRangOfList(lstMonthOfIssuedDate, 1, 12);
SetRangOfList(lstMonthOfStartDate, 1, 12);
SetRangOfList(lstMonthOfProbEnd, 1, 12);
SetRangOfList(lstYearOfDateOfBirth, 1970, 2007);
SetRangOfList(lstYearOfEndDate, 2000, 2020);
SetRangOfList(lstYearOfProbEnd, 2000, 2020);
SetRangOfList(lstYearOfStartDate, 2000, 2020);
SetRangOfList(lstYearOfIssuedDate, 2000, 2020);
objEmployeeDetails =
objEmployeeDB.GetEmployee(Request.QueryString["employeeID"]);
if(!IsPostBack) {
if (objEmployeeDB.Error == 0)
{
this.txtEmployeeID.Text = objEmployeeDetails.EmployeeID;
this.txtFirstName.Text = objEmployeeDetails.FirstName;
this.txtLastName.Text = objEmployeeDetails.LastName;
this.lstDayOfDateOfBirth.Text =
objEmployeeDetails.DateOfBirth.Day.ToString();
this.lstMonthOfDateOfBirth.Text =
objEmployeeDetails.DateOfBirth.Month.ToString();
this.lstYearOfDateOfBirth.Text =
objEmployeeDetails.DateOfBirth.Year.ToString();
this.txtPernamentAddress.Text =
objEmployeeDetails.PernamentAddress;
this.txtIDCardNumber.Text = objEmployeeDetails.IDCardNumber;
this.lstDayOfIssuedDate.Text =
objEmployeeDetails.IssuedDate.Day.ToString();
this.lstMonthOfIssuedDate.Text =
objEmployeeDetails.IssuedDate.Month.ToString();
this.lstYearOfIssuedDate.Text =
objEmployeeDetails.IssuedDate.Year.ToString();
this.txtIssuedPlace.Text = objEmployeeDetails.IssuedPlace;
this.txtTitle.Text = objEmployeeDetails.Title;
this.txtDepartment.Text = objEmployeeDetails.Department;
this.lstDayOfStartDate.Text =
objEmployeeDetails.StartDate.Day.ToString();
this.lstMonthOfStartDate.Text =
objEmployeeDetails.StartDate.Month.ToString();
this.lstYearOfStartDate.Text =
objEmployeeDetails.StartDate.Year.ToString();
this.lstDayOfProbEnd.Text =
objEmployeeDetails.ProbEnd.Day.ToString();
this.lstMonthOfProbEnd.Text =
objEmployeeDetails.ProbEnd.Month.ToString();
this.lstYearOfProbEnd.Text =
objEmployeeDetails.ProbEnd.Year.ToString();
this.lstDayOfEndDate.Text =
objEmployeeDetails.EndDate.Day.ToString();
this.lstMonthOfEndDate.Text =
objEmployeeDetails.EndDate.Month.ToString();