You are on page 1of 8

一、实验目的:

Request 对象(客户端的请求信息)
Request 对象的常用属性和方法
Response 对象(Web 服务器对客户端请求的响应)
Response 对象的常用属性和方法
Cookie 对象(把少量数据储存到客户端的硬盘或内存中,或是从客户端硬盘读取数据的一
种技术)
Cookie 对象的常用属性和方法
Session 对象
Session 对象的常用属性和方法
Application 对象保存所有客户的信息
Application 对象的常用属性和方法
实现在线人数的统计
二、实验内容:
1、启动 Visual Studio 2010 执行“文件”“新建”“项目”命令,打开如图所示的“新建
项目”对话框。打开“Visual C#”类型节点,选择“Web”子节点这个模板,同时在右边窗
口中选择“ASP.NET 空 Web 应用程序”,在“名称”文本框中输入“任务 4.8.2”,并在
“位置”文本框中输人相应的存储路径。最后,单击“确定”按钮。
2、登录及注册功能实现代码见“任务 4.7”,这里不再赘述。
register.aspx:
<form id="form1" runat="server">
<table width="1200px" align="center" border="0"
<tr><td width="400px" class="auto-style2" align="right"> 用 户 姓 名 : </td><td
width="400px" class="auto-style2">
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</td><td width="400px" class="auto-style2">
<asp:Button ID="jiance" runat="server" Text=" 检 测 是 否 可 用 "
OnClick="jiance_Click" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtUserName" ErrorMessage=" 用 户 名 不 能 为 空 "
ForeColor="Red"></asp:RequiredFieldValidator>
</td></tr>
<tr><td width="400px" align="right" class="auto-style5"> 密码:</td><td width="400px"
class="auto-style5">
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox>
</td><td width="400px" class="auto-style5">
<asp:RequiredFieldValidator ID="RequiredFieldValidatorPwd" runat="server"
ControlToValidate="txtPwd" ErrorMessage=" 密 码 不 能 为 空 "
ForeColor="Red"></asp:RequiredFieldValidator>
</td></tr>
<tr><td width="400px" align="right" class="auto-style4"> 确 认 密 码 : </td><td
width="400px" class="auto-style4">
<asp:TextBox ID="txtPwd2" runat="server" TextMode="Password"></asp:TextBox>
</td><td width="400px" class="auto-style4">
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="txtPwd" ControlToValidate="txtPwd2" ErrorMessage="两次输入的密码不
一致" ForeColor="Red"></asp:CompareValidator>
</td></tr>
</table>
</form>

Register.cs:
namespace User_Register
{
public partial class Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UnobtrusiveValidationMode = new UnobtrusiveValidationMode();
if (this.txtPwd.Text != " ")
{
this.txtPwd.Attributes["value"] = this.txtPwd.Text;
}
if (this.txtPwd2.Text != " ")
{
this.txtPwd2.Attributes["value"] = this.txtPwd2.Text;
}
}

protected void jiance_Click(object sender, EventArgs e)


{
string sql = " select count(*) from dbo.tb_user where UserName=N' " +
txtUserName.Text.ToString() + " ' ";
int i = int.Parse(DB.SelOne(sql));
if(i == 0)
{
Response.Write("<script language ='javascript'>alert(' 恭 喜 你 , 用 户 名 可
用')</script>");
}
else
{
Response.Write("<script language ='javascript'>alert(' 对 不 起 , 用 户 名 不 可
用')</script>");
}
}

protected void tijiao_Click(object sender, EventArgs e)


{
string sql1 = "select count(*) from dbo.tb_user where UserName=N' " + txtUserName.Text
+ " ' ";
int i = int.Parse(DB.SelOne(sql1));
if (i == 0)
{
string gender = null;
if (boy.Checked)
{
gender = "男";
}
else
{
gender = "女";
}
string sql = "insert into
dbo.tb_user(UserName,PassWord,PassWord2,TrueName,Adress,Tel,Postcode,sex) values(N' " +
txtUserName.Text + " ' , ' " + txtPwd.Text + " ' , ' " + txtPwd2.Text + " ' , ' " + txtName.Text + " ' , '
" + txtAddress.Text + " ' , ' " + txtTel.Text + " ' , ' " + txtPostcode.Text + " ' , ' " + gender + " ')";
DB.ExecSql(sql);
Response.Write("<script language ='javascript'>alert('提交成功')</script>");
Response.Redirect("Default.aspx");
}
else
{
Response.Write("<script language ='javascript'>alert(' 对 不 起 , 用 户 名 不 可
用')</script>");
}
}

protected void chongzhi_Click(object sender, EventArgs e)


{
txtUserName.Text = " ";
txtPwd.Text = " ";
txtPwd2.Text = " ";
txtName.Text = " ";
txtAddress.Text = " ";
txtTel.Text = " ";
txtPostcode.Text = " ";
boy.Checked = true;
girl.Checked = true;

}
}
}
用户登录.aspx:
<form id="form1" runat="server">
用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br/>
密 码 :<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
<asp:Button ID="Button1" runat ="server" onclick="Button1_Click" Text="登录"/><br/>
<asp:CheckBox ID="CheckBox1" runat ="server" Text="记住用户密码"/>
<div>
</div>
</form>

用户登录.cs:
namespace 用户登录
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["ID"] != null && Request.Cookies["PWD"] != null)
{
string id = Request.Cookies["ID"].Value.ToString();
string pwd = Request.Cookies["PWD"].Value.ToString();
Response.Redirect("New.aspx? ID=" +id+"&PWD="+pwd);
}
}

protected void Button1_Click(object sender, EventArgs e)


{
if (CheckBox1.Checked)
{
Response.Cookies["ID"].Expires = DateTime.Now.AddDays(30);
Response.Cookies["PWD"].Expires = DateTime.Now.AddDays(30);
Response.Cookies["ID"].Value = TextBox1.Text;
Response.Cookies["PWD"].Value = TextBox2.Text;
}
Response.Redirect("New.aspx? ID=" +TextBox1.Text+ "&PWD=" + TextBox2.Text);
}
}
}
3) 在“解决方案资源管理器”窗口中,右击项目选择快捷菜单“添加新项”命令。在
“添加新项”窗口中选择“全局应用程序类”模板,并将文件命名为 Global.asax,单击
“添加”按钮。
4)双击 Global.asax.cs 文件,输人如下代码:
protected void Application_Start (object sender, EventArgs e)
{
Application[" user_sessions" ] = 0;
}
protected void Session_Start (object sender, EventArgs e)
{
Application. Lock (;
Application[" user_sessions" ] = (int) Application[" user _sessions" ] + 1;
Application. UnLock () ;
}
protected void Session_End(onject sender, EventArgs e)
}
Application. Lock () ;
Application[" user_sessions" ] = (int) Application [" user_sessions" ] - 1;
Application. UnLock ();
}

教材 P117 创建用户注册页面

Register.cs
namespace User_Register
{
public partial class Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UnobtrusiveValidationMode = new UnobtrusiveValidationMode();
if (this.txtPwd.Text != " ")
{
this.txtPwd.Attributes["value"] = this.txtPwd.Text;
}
if (this.txtPwd2.Text != " ")
{
this.txtPwd2.Attributes["value"] = this.txtPwd2.Text;
}
}

protected void jiance_Click(object sender, EventArgs e)


{
string sql = " select count(*) from dbo.tb_user where UserName=N' " +
txtUserName.Text.ToString() + " ' ";
int i = int.Parse(DB.SelOne(sql));
if(i == 0)
{
Response.Write("<script language ='javascript'>alert(' 恭 喜 你 , 用 户 名 可
用')</script>");
}
else
{
Response.Write("<script language ='javascript'>alert(' 对 不 起 , 用 户 名 不 可
用')</script>");
}
}

protected void tijiao_Click(object sender, EventArgs e)


{
string sql1 = "select count(*) from dbo.tb_user where UserName=N' " + txtUserName.Text
+ " ' ";
int i = int.Parse(DB.SelOne(sql1));
if (i == 0)
{
string gender = null;
if (boy.Checked)
{
gender = "男";
}
else
{
gender = "女";
}
string sql = "insert into
dbo.tb_user(UserName,PassWord,PassWord2,TrueName,Adress,Tel,Postcode,sex) values(N' " +
txtUserName.Text + " ' , ' " + txtPwd.Text + " ' , ' " + txtPwd2.Text + " ' , ' " + txtName.Text + " ' , '
" + txtAddress.Text + " ' , ' " + txtTel.Text + " ' , ' " + txtPostcode.Text + " ' , ' " + gender + " ')";
DB.ExecSql(sql);
Response.Write("<script language ='javascript'>alert('提交成功')</script>");
Response.Redirect("Default.aspx");
}
else
{
Response.Write("<script language ='javascript'>alert(' 对 不 起 , 用 户 名 不 可
用')</script>");
}
}

protected void chongzhi_Click(object sender, EventArgs e)


{
txtUserName.Text = " ";
txtPwd.Text = " ";
txtPwd2.Text = " ";
txtName.Text = " ";
txtAddress.Text = " ";
txtTel.Text = " ";
txtPostcode.Text = " ";
boy.Checked = true;
girl.Checked = true;

}
}
}

DB.cs:
namespace User_Register.App_Code
{
public class DB
{
public static SqlConnection DBconnection()
{
return new SqlConnection("Data Source=(LocalDB)\\
MSSQLLocalDB;AttachDbFilename=\"E:\\ 桌 面 \\User Register\\App_Data\\DB.mdf\";Integrated
Security=True;");
}

public static DataTable ExecSel(string sql)


{
SqlConnection con = DB.DBconnection();
con.Open();
SqlCommand com=new SqlCommand(sql,con);
SqlDataAdapter oda =new SqlDataAdapter();
DataSet ds =new DataSet();
oda.SelectCommand = com;
oda.Fill(ds);
con.Close();
return ds.Tables[0];
}

public static void ExecSql(string sql)


{
SqlConnection con = DB.DBconnection();
con.Open();
SqlCommand com =new SqlCommand(sql,con);
com.ExecuteNonQuery();
con.Close();
}
public static string SelOne(string sql)
{
SqlConnection con = DB.DBconnection();
con.Open();
SqlCommand com = new SqlCommand(sql, con);
string result = com.ExecuteScalar().ToString();
con.Close();
return result;
}
}
}

Tb_user:
CREATE TABLE [dbo].[tb_user] (
[UserName] NVARCHAR (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[PassWord] NVARCHAR (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[PassWord2] NVARCHAR (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[TrueName] NVARCHAR (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Adress] NVARCHAR (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Tel] NVARCHAR (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Postcode] NVARCHAR (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[sex] NVARCHAR (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
PRIMARY KEY CLUSTERED ([UserName] ASC)
);

You might also like