前言
为了帮助同学们完成痛苦的实验课程设计,本作者将其作出的实验结果及代码贴至CSDN中,供同学们学习参考。如有不足或描述不完善之处,敬请各位指出,欢迎各位的斧正!
一、实验目的
1.掌握HttpRequest对象的应用;
2.掌握HttpResponse对象的应用;
3.掌握跨页面数据传递的方法;
4.掌握Cookie、Session、Application应用。
二、实验环境
Visual Studio 2019 ASP.Net Framework
三、实验内容
1.实现跨页面数据传递的用户注册。要求如下:
(1)当页面首次加载时显示注册页面。
(2)当用户提交注册后,在确认页面获取注册页面用户输入提交的表单信息并在页面中显示,并在显示的表单信息下面显示一个“确认”按钮。
(3)单击“确认”按钮,出现注册成功的提示框。
2.设计并实现一个简易的聊天室。
3.设计并实现一个简易的购物车。
四、代码及截图
实验一:
WebForm1.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 454px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:Label ID="Label1" runat="server" Text="注册通行证" Font-Size="XX-Large" Font-Bold="True" Width="220px" style="text-align:center"></asp:Label>
<br />
<br />
</div>
<table>
<tr><td><asp:Label ID="Label2" runat="server" Text="姓 名" Width=70px></asp:Label></td><td class="auto-style1"><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td> </tr>
<tr><td><asp:Label ID="Label3" runat="server" Text="密 码" Width=70px></asp:Label></td><td class="auto-style1"><asp:TextBox ID="TextBox2" runat="server" TextMode=Password></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="请输入密码!" Display="Dynamic" ControlToValidate="TextBox2" SetFocusOnError="true"></asp:RequiredFieldValidator>
</td></tr>
<tr><td><asp:Label ID="Label6" runat="server" Text="确认密码" Width=70px></asp:Label></td><td class="auto-style1"><asp:TextBox ID="TextBox4" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="请输入确认密码!" ControlToValidate="Textbox4" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator><asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="密码与确认密码不一致!" ControlToCompare="TextBox2" ControlToValidate="TextBox4" Display="Dynamic" SetFocusOnError="true"> </asp:CompareValidator>
</td></tr>
<tr><td><asp:Label ID="Label5" runat="server" Text="提 问" Width=70px></asp:Label></td><td class="auto-style1"><asp:DropDownList ID="DropDownList1" runat="server" Width="173px">
<asp:ListItem>你的出生地</asp:ListItem>
</asp:DropDownList></td></tr>
<tr><td><asp:Label ID="Label4" runat="server" Text="回 答" Width=70px></asp:Label></td><td class="auto-style1"><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="请输入问题的回答!" ControlToValidate="TextBox3" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td></tr>
</table>
<asp:Button ID="Button1" runat="server" Text="注册" onclick="Button1_Click" PostBackUrl="~/web/WebForm2.aspx"/>
</form>
</body>
</html>
界面设计:
WebForm1.aspx.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
public string getname()
{
return TextBox1.Text.Trim();
}
public string getpwd()
{
return TextBox2.Text.Trim();
}
public string getask()
{
return DropDownList1.SelectedItem.Text.ToString();
}
public string getans()
{
return TextBox3.Text.Trim();
}
}
}
WebForm2.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication4.WebForm2" %>
<%@ PreviousPageType VirtualPath="~/web/WebForm1.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
</div>
<br />
<asp:Button ID="Button1" runat="server" Text="确认" OnClientClick="this.disabled=true;alert('注册成功!');"/>
<asp:Button ID="Button2" runat="server" Text="返回" PostBackUrl="~/web/WebForm1.aspx"/>
</form>
</body>
</html>
界面设计:
WebForm2.aspx.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
namespace WebApplication4
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
firstload();
}
protected void firstload()
{
if (PreviousPage.IsCrossPagePostBack)
{
Label1.Text = "来自" + /*Request.UserHostAddress.ToString()*/IPNetworking.GetIP4Address() + "的朋友,您好!" + "<br />" + "<br />";
Label1.Text += "以下是您提交的信息,请确认!" + "<br />" + "<br />";
Label1.Text += "用户名:" + PreviousPage.getname() + "<br />";
Label1.Text += "密码:" + PreviousPage.getpwd() + "<br />";
Label1.Text += "安全提示问题:" + PreviousPage.getask() + "<br />";
Label1.Text += "安全回答:" + PreviousPage.getans() + "<br />";
}
}
public class IPNetworking
{
public static string GetIP4Address()
{
string IP4Address = String.Empty;
foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
{
if (IPA.AddressFamily.ToString() == "InterNetwork")
{
IP4Address = IPA.ToString();
break;
}
}
if (IP4Address != String.Empty)
{
return IP4Address;
}
foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (IPA.AddressFamily.ToString() == "InterNetwork")
{
IP4Address = IPA.ToString();
break;
}
}
return IP4Address;
}
}
}
}
运行截图:
不输入确认密码
确认密码输入错误
不输入回答
单击注册
单击确认
单击返回
实验二:
Chatlogin.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="chatlogin.aspx.cs" Inherits="exp2_2.chatlogin" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>
<asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label>
</td><td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td><td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" Text="*"></asp:RequiredFieldValidator>
</td></tr>
<tr><td>
<asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>
</td><td>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
</td><td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="RequiredFieldValidator" Text="*"></asp:RequiredFieldValidator>
</td></tr>
<tr><td></td><td>
<asp:Button ID="Button1" runat="server" Text="登录" OnClick="Button1_Click" Width="66px" />
</td><td></td></tr>
</table>
</div>
</form>
</body>
</html>
界面设计:
Chatlogin.aspx.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace exp2_2
{
public partial class chatlogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
TextBox1.Focus();
}
string[,] user = { { "张三", "123" },{ "李四", "456" },{ "王五", "789" }};
protected void Button1_Click(object sender, EventArgs e)
{
for(int i=0;i<=2;i++)
{
if(TextBox1.Text==user[i,0]&&TextBox2.Text==user[i,1])
{
Session["user"] = user[i, 0];
Response.Redirect("chat.html");
}
}
Response.Write("<script type='text/javascript'>alert('用户名或密码错误!');</script>");
}
}
}
Chatdisplay.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="chatdisplay.aspx.cs" Inherits="exp2_2.chatdisplay" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="refresh" content="0.5" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
界面设计:
Chatdisplay.aspx.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace exp2_2
{
public partial class chatdisplay : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Application["message"].ToString();
}
}
}
Sendmessage.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="sendmessage.aspx.cs" Inherits="exp2_2.sendmessage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="发送" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="清空" OnClick="Button2_Click" />
</div>
</form>
</body>
</html>
界面设计:
Sendmessage.aspx.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace exp2_2
{
public partial class sendmessage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "发言人:" + Session["user"];
if(!IsPostBack)
{
Application["message"] += Session["user"] + "进入聊天室<br />";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Application.Lock();
Application["message"] += Session["user"] + ":" + TextBox1.Text + "(" + DateTime.Now.ToString() + ")<br />";
Application.UnLock();
TextBox1.Text = "";
}
protected void Button2_Click(object sender, EventArgs e)
{
Application.Lock();
Application["message"] = "";
Application.UnLock();
TextBox1.Text = "";
}
}
}
Chat.html代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-franeset.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html"; charset="gb2312" />
<title>我的聊天室</title>
</head>
<frameset rows="20%,25%">
<frame name="top" src="chatdisplay.aspx"></frame>
<frame name="bottom" src="sendmessage.aspx">
<noframes><body>
<p>此网页使用了框架,但您的浏览器不支持框架。</p>
</body></noframes>
</frame>
</frameset>
</html>
Global.asax代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace exp2_2
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Application["message"] = "";
}
}
}
错误的用户名或密码:
正确的用户名和密码:
单击清空:
实验三:
Default.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="exp2_3._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>盲鱼</asp:ListItem>
<asp:ListItem>波斯猫</asp:ListItem>
<asp:ListItem>斑马</asp:ListItem>
<asp:ListItem>千里马</asp:ListItem>
<asp:ListItem>绵羊</asp:ListItem>
</asp:CheckBoxList>
<br />
<asp:Button ID="Button1" runat="server" Text="放入购物车" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="查看购物车" OnClick="Button2_Click" />
</div>
</form>
</body>
</html>
界面设计:
Default.aspx.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace exp2_3
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
for(int i=0;i<CheckBoxList1.Items.Count;i++)
{
if(CheckBoxList1.Items[i].Selected)
{
Session["cart"] += CheckBoxList1.Items[i].Text + ",";
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("viewcart.aspx");
}
}
}
Viewcart.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="viewcart.aspx.cs" Inherits="exp2_3.viewcart" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
<asp:Button ID="Button1" runat="server" Text="清空购物车" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="继续购物" OnClick="Button2_Click" />
<br />
</div>
</form>
</body>
</html>
界面设计:
Viewcart.aspx.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace exp2_3
{
public partial class viewcart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
if(Session["cart"] == null)
{
Label1.Text = "没有选择任何宠物!";
Button1.Enabled = false;
}
else
{
string strpets = Session["cart"].ToString();
System.Collections.ArrayList pets = new System.Collections.ArrayList();
int iposition = strpets.IndexOf(",");
while(iposition!=-1)
{
string strpet = strpets.Substring(0, iposition);
if (strpet != "")
{
pets.Add(strpet);
strpets = strpets.Substring(iposition + 1);
iposition = strpets.IndexOf(",");
}
}
Label1.Text = "购物车现有宠物:";
CheckBoxList1.DataSource = pets;
CheckBoxList1.DataBind();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session.Remove("cart");
Label1.Text = "没有选购任何宠物!";
CheckBoxList1.Visible = false;
Button1.Enabled = false;
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
}
}
运行:
点击放入购物车、查看购物车:
点击清空购物车:
点击继续购物:
放入购物车:
点击继续购物:
五、实验总结
在实验二的编程过程中,出现了未将对象引用设置到对象的实例的情况,界面效果令人不甚满意。在经过查阅资料并进行多次调试与改进后,发现是因为PreviousPage.IsCrossPagePostBack与Response.write出现冲突,于是我们修改了response.write并将其修改到了其aspx的源界面,最终达到了较为满意的界面效果。