您的位置:首页 > 编程语言 > ASP

asp.net 实验五 内置对象与缓存

2013-11-20 15:53 411 查看
一. 目的和要求

掌握Application, Session, Request, Response, Server等各种内置对象的使用。掌握Cookie对象的使用。

二.实验课时

2课时。

三.实验内容

1. 编程实现在线人数的统计。

2. 用Session对象实现网页访问计数器

3. 编写一个页面让用户选择喜欢的颜色,下次进入该页面,能以用户喜欢的颜色显示文字。

4. 创建三个包含三个Web窗体的 Web应用程序,即default.aspx,default1.aspx和default2.aspx.创建default.aspx,如图所示:

1) 如果用户只输入一个参数(姓名或国家),则必须执行default1.aspx.该Web窗体必须显示消息”您需要输入所有必填信息”。
2) 如果用户同时输入这两个参数,则必须将控制转移到default2.aspx.该Web窗体必须显示SessionID和会话超时。

//1-3

<%@Page Language="C#"%>
<html>
<body bgcolor="<%=Application["color"]%>" >
<form id="form1" runat="server">
<asp:Label id="visitors" runat="server"/>
<asp:Label id="person_num" runat="server"/> <br/>
<asp:Button id="btn_red" runat="server" BackColor="#FF0000" OnClick="btn_color"/>
<asp:Button id="btn_yellow" runat="server" BackColor="#FFF000" OnClick="btn_color"/>
</form>
</body>
</html>

using System;
using System.Web;
using System.Web.UI;
namespace asptext
{
public partial class Default : System.Web.UI.Page
{
private void Page_Load (object sender, System.EventArgs e)
{
visitors.Text = "本站当前有:" + (Application ["user_sessions"]).ToString () + "" + "位访问者 !";
person_num.Text = "<br/>你是第" + (Application ["person_num"] = (int)Application ["person_num"] + 1).ToString () + "个人";
}
protected void btn_color(object sender, System.EventArgs e)
{
Button sendButton = (Button)sender;
Application["color"]=sendButton.BackColor;
}
}
}

//Global
protected virtual void Application_Start (Object sender, EventArgs e)
{
Application["user_sessions"] = 0;
Application["person_num"]=0;
Application["color"]="#FF0000";
}
protected virtual void Session_Start (Object sender, EventArgs e)
{
Application["user_sessions"] = (int)Application["user_sessions"] + 1;
}
protected virtual void Session_End (Object sender, EventArgs e)
{
Application["user_sessions"] = (int)Application["user_sessions"] - 1;
}


//4 Default
<%@Page Language="C#"%>
<html>
<body>
<form id="form1" runat="server">
<asp:Button id="button1" runat="server" Text="Click me!" OnClick="button1Clicked"/><br/>
Name:<asp:TextBox id="txb_name" runat="server"/><br/>
Country:<asp:TextBox id="txb_country" runat="server"/><br/>
</form>
</body>
</html>

using System;
using System.Web;
using System.Web.UI;
namespace aspInter
{
public partial class Default : System.Web.UI.Page
{
public virtual void button1Clicked (object sender, EventArgs args)
{
button1.Text = "You clicked me";
int num = 0;
if(txb_name.Text.Length != 0) num++;
if(txb_country.Text.Length != 0) num ++;
if(num == 0) Response.Write ("请输入姓名和国家<br/>");
if(num == 1)
{
Response.Redirect("Default1.aspx");
return;
}
Session.Add("name",txb_name.Text);
Session.Add("country",txb_country.Text);
Response.Redirect("Default2.aspx");
}
}
}

//Default2
<%@ Page Language="C#"%>
<html>
<body>
<form id="form1" runat="server">
您需要输入所有必填信息!<br/>
</form>
</body>
</html>
//Default3
<%@Page Language="C#"%>
<html>
<body>
<form id="form1" runat="server">
ID:<%=Session.SessionID%><br/>
Time:<%=Session.Timeout%>
</form>
</body>
</html>

6. 实验思考题:

1) Server的UrlEncode(),MapPath()和HTMLEncode()各起什么作用?
Server.UrlEncode()方法; // URL对字符串进行编码。
Server.MapPath()方法:获取ASP应用程序运行的物理绝对路
Server.HtmlEncode()方法; //对字符串进行HTML编码。

2) Global.asax中有哪些常用事件?什么时候触发这些事件?
Application_Start:请求 ASP.NET 应用程序中第一个资源(如页)时调用。
Application_End:在卸载应用程序之前对每个应用程序生命周期调用一次。
Session_Start:如果请求开始一个新会话时运行。
Session_End:在 Abandon 方法已被调用或会话已过期时运行。
Application_BeginRequest:在收到Request时第一个触发的事件。
Application_EndRequest:每次请求结束时会引发的事件。
Application_Error:所有没有处理的错误都会导致这个方法的执行。
HttpModule:ASP.NET 应用程序生命周期可通过 IHttpModule 类进行扩展。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ASP.NET