Application应用
1.建立一个系统默认的页面,建立2个文本控件和两个按钮控件
2.在系统默认的页面代码文件中输入以下代码。
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Application.Contents["StartTime"]);
Response.Write(Request.QueryString["name1"]);//在页面上输出所提交的用户信息。
Response.Write(Server.UrlDecode(Request.QueryString["name2"]));
}
protected void Button1_Click(object sender, EventArgs e)
{
string strName = TextBox1.Text;//定义一个字符串,把文本框的文本放入字符串。
Response.Redirect("Default.aspx?name1=" + strName);//Response.Redirect指向所指定的页面。
}
protected void Button2_Click(object sender, EventArgs e)
{
string strName1 = Server.UrlEncode(TextBox2.Text);
Response.Redirect("Default.aspx?name2=" + strName1);
}
}
3.建立Global.asax全局应用程序类文件
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
Application.Add("StartTime", System.DateTime.Now.ToString()); // 在应用程序启动时运行的代码
}
void Application_End(object sender, EventArgs e)
{
Application.Add("EndTime", System.DateTime.Now.ToString());// 在应用程序关闭时运行的代码
}
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
}
void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
}
</script>
//黑色代码为系统默认代码
.NET 入门教程:使用全局应用程序类
这篇博客介绍了在.NET环境中如何创建和使用全局应用程序类(Global.asax)。首先,通过创建一个简单的网页,展示了如何在Page_Load事件中获取和显示Application对象的数据。接着,解释了Button1_Click和Button2_Click事件处理程序,用于处理用户输入并重定向。最后,详细说明了如何在Global.asax文件中定义Application_Start、Application_End等生命周期方法,以便在应用程序启动和关闭时执行特定操作。
2073

被折叠的 条评论
为什么被折叠?



