網(wǎng)站前臺(tái)之用戶注冊(cè)和登陸
第六章?網(wǎng)站前臺(tái)之用戶注冊(cè)和登陸?1、首先新建用戶注冊(cè)頁(yè)面Register.aspx ,添加注冊(cè)時(shí)需要的相應(yīng)控件,在注冊(cè)代碼中使用了四種類(lèi)型的驗(yàn)證,具體如下:?(1)、判空驗(yàn)證?*用戶名:(2)、比較
第六章?網(wǎng)站前臺(tái)之用戶注冊(cè)和登陸?
1、首先新建用戶注冊(cè)頁(yè)面Register.aspx ,添加注冊(cè)時(shí)需要的相應(yīng)控件,在注冊(cè)代碼中使用了四種類(lèi)型的驗(yàn)證,具體如下:?
(1)、判空驗(yàn)證?
*用戶名:
ControlToValidate ="txtName">
(2)、比較驗(yàn)證?
*確認(rèn)密碼
ControlToValidate ="txtRePwd">
(3)、范圍驗(yàn)證?
*年齡
runat ="server" Width ="27px">
歲 ErrorMessage ="輸入1-120之間的數(shù)字" Type ="Integer" ControlToValidate ="txtAge" MaximumValue ="120" MinimumValue ="1">
ControlToValidate ="txtAge">
(4)、正則表達(dá)式驗(yàn)證?
*郵箱
ControlToValidate ="txtEmail">
ValidationExpression
運(yùn)行效果如下:?
?
,2、在填寫(xiě)出生日期的時(shí)候,最好是提供選擇,首先需要引用一個(gè)Calendar 的js 文件,然后在文本框中添加onclick 的事件調(diào)用腳本,具體代碼如下:? ");
//成功后提示再跳轉(zhuǎn)
}
else
{
BLL. Users userBll = new BLL.Users ();
Model. Users userModel = new Model.Users ();
userModel.userBirthday =Convert .ToDateTime( txtBirthday.Text.Trim());
userModel.userImg = "" ;
userModel.userName = txtName.Text.Trim();
,userModel.userPassword = txtPwd.Text.ToString().Trim();
userModel.userEmail = txtEmail.Text.Trim();
userModel.userRegisterTime = DateTime .Now;
userModel.userSex = Convert .ToInt32(radio.SelectedItem.Value) == 0 ? false : true ; userModel.userState = "0" ;
userModel.userTel = txtPhone.Text.Trim();
userModel.userLastLoginTime = DateTime .Now;
if (userBll.Add(userModel) > 0)
{
ClientScript.RegisterStartupScript(this .GetType(), "success" , "");
//成功后提示再跳轉(zhuǎn)
}
}
}
運(yùn)行效果如下:

?

?
?
,6、注冊(cè)功能完成后,如果需要進(jìn)行一些其他特殊操作,會(huì)員需要登陸到系統(tǒng),接下來(lái)演示登陸功能的實(shí)現(xiàn),首先新建Login.aspx 的頁(yè)面,進(jìn)行頁(yè)面布局,具體代碼如下:?
用戶名: | ErrorMessage ="不能為空" ControlToValidate ="txtName"> | |
密碼: | ErrorMessage ="不能為空" ControlToValidate ="txtPwd"> | |
運(yùn)行效果圖如下:

?
7、前臺(tái)代碼寫(xiě)完之后,開(kāi)始編寫(xiě)登陸事件的后臺(tái)代碼, 具體代碼如下:?protected void Button1_Click(object sender, EventArgs e)
{
BLL. Users userBll = new BLL.Users ();
string userName = txtName.Text.Trim();
string userPwd = txtPwd.Text.Trim();
DataSet ds=new DataSet ();
ds = userBll.GetList(" userName='" userName "' and userPwd='" userPwd "'" ); if (ds != null )
{
if (ds.Tables[0].Rows.Count > 0)
{
ClientScript.RegisterStartupScript(this .GetType(), "success" , "");
//成功后提示再跳轉(zhuǎn)
}
else
{
ClientScript.RegisterStartupScript(this .GetType(), "success" , "");
//成功后提示再跳轉(zhuǎn)
}
}
else
{
ClientScript.RegisterStartupScript(this .GetType(), "success" , "");
//成功后提示再跳轉(zhuǎn)
}
}
運(yùn)行效果圖如下:?
,?
?
8、登陸成功后,跳轉(zhuǎn)進(jìn)入個(gè)人空間,可以進(jìn)行個(gè)人頭像的修改,具體代碼如下:?Model. Users userModel = new Model.Users ();
BLL. Users userBll = new BLL.Users ();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["userModel" ] != null )
{
userModel = (Model.Users )Session["userModel" ];
if (userModel.userImg == "" ||userModel.userImg ==null )
{
Image1.ImageUrl = "../Images/admin/avatar.png";
}
else
{
Image1.ImageUrl = "../Images/admin/" userModel.userImg;
}
lblName.Text = userModel.userName;
lblPhone.Text = userModel.userTel.Trim();
,lblSex.Text = userModel.userSex == false ? " 女" : " 男" ;
}
else
{
ClientScript.RegisterStartupScript(this .GetType(), "success" , "");
//成功后提示再跳轉(zhuǎn)
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
userModel = (Model.Users )Session["userModel" ];
if (FileUpload1.HasFile == false )
{
ClientScript.RegisterStartupScript(this .GetType(), "success" , "");
return ;
}
string IsXls = System.IO.Path .GetExtension(FileUpload1.FileName).ToString().ToLower(); if (IsXls != ".jpg" )
{
ClientScript.RegisterStartupScript(this .GetType(), "success" , "");
return ;
}
string str = (DateTime .Now).ToString("yyyyMMddHHmmss" );
string FileName = str IsXls;
try
{
string savePath;
savePath = Server.MapPath("../Images/admin/" FileName);
FileUpload1.SaveAs(savePath);
//UsersModel.userId = int.Parse(Session["userId"].ToString());
userModel.userImg = FileName;
//usersBLL.Add(UsersModel);
bool bl= userBll.Update(userModel);