当前位置:首页 > .NET内置对象之Application对象

.NET内置对象之Application对象

点击次数:1205  更新日期:2010-12-29
\n

Application对象


\n

Application对象是HttpApplicationState类的一个实例,它可以产生一个所有Web应用程序都可以存取的变量,这个变量的可以存取范围涵盖全部使用者,也就是说只要正在使用这个网页的程序都可以存取这个变量。


\n

新建一个网站,包括两个网页,代码如下:


\n

1、Index.aspx代码:


\n

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Index.aspx.cs” Inherits=”Index” %>


\n

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>


\n

<html xmlns=”http://www.w3.org/1999/xhtml” >


\n

<head runat=”server”>


\n

<title>无标题页</title>


\n

</head>


\n

<body>


\n

<form id=”form1″ runat=”server”>


\n

<div>


\n

<table style=”width: 300px; height: 100px”>


\n

<tr>


\n

<td style=”width: 100px”>


\n

用户名:</td>


\n

<td style=”width: 246px”>


\n

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox></td>


\n

</tr>


\n

<tr>


\n

<td style=”width: 100px”>


\n

密 &nbsp;&nbsp; 码:</td>


\n

<td style=”width: 246px”>


\n

<asp:TextBox ID=”TextBox2″ runat=”server” TextMode=”Password”></asp:TextBox></td>


\n

</tr>


\n

<tr>


\n

<td style=”text-align: center;” colspan=”2″>


\n

<asp:Button ID=”Button1″ runat=”server” Text=”登录” onClick=”Button1_Click” /></td>


\n

</tr>


\n

</table>


\n

</div>


\n

</form>


\n

</body>


\n

</html>


\n

Index.aspx.cs代码:


\n

using System;


\n

using System.Data;


\n

using System.Configuration;


\n

using System.Collections;


\n

using System.Web;


\n

using System.Web.Security;


\n

using System.Web.UI;


\n

using System.Web.UI.WebControls;


\n

using System.Web.UI.WebControls.WebParts;


\n

using System.Web.UI.HtmlControls;


\n


\n

public partial class Index : System.Web.UI.Page


\n

{


\n

string strInfo;


\n

protected void Page_Load(object sender, EventArgs e)


\n

{


\n


\n

}


\n

protected void Button1_Click(object sender, EventArgs e)


\n

{


\n

Application["Info"] = TextBox1.Text;


\n

strInfo = Application["Info"].ToString();


\n

if (TextBox1.Text == “admin” && TextBox2.Text == “admin”)


\n

{


\n

Session["name"] = TextBox1.Text;


\n

Response.Redirect(“Default.aspx?Info=” + strInfo + “”);//地址栏的传值


\n

}


\n

}


\n

}


\n

2、Default.aspx代码:


\n

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>


\n


\n

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>


\n


\n

<html xmlns=”http://www.w3.org/1999/xhtml” >


\n

<head runat=”server”>


\n

<title>无标题页</title>


\n

</head>


\n

<body>


\n

<form id=”form1″ runat=”server”>


\n

<div>


\n

<table style=”width: 500px; height: 323px”>


\n

<tr>


\n

<td colspan=”2″>


\n

<asp:TextBox ID=”TextBox2″ runat=”server” Height=”314px” Width=”497px” TextMode=”MultiLine”></asp:TextBox></td>


\n

</tr>


\n

<tr>


\n

<td>


\n

<asp:Label ID=”Label1″ runat=”server” Text=”Label”></asp:Label>


\n

<asp:TextBox ID=”TextBox1″ runat=”server” Width=”390px”></asp:TextBox></td>


\n

<td>


\n

&nbsp;


\n

<asp:Button ID=”Button2″ runat=”server” Text=”发送” onClick=”Button2_Click” /></td>


\n

</tr>


\n

</table>


\n

</div>


\n

</form>


\n

</body>


\n

</html>


\n

Default.aspx.cs代码:


\n

using System;


\n

using System.Data;


\n

using System.Configuration;


\n

using System.Web;


\n

using System.Web.Security;


\n

using System.Web.UI;


\n

using System.Web.UI.WebControls;


\n

using System.Web.UI.WebControls.WebParts;


\n

using System.Web.UI.HtmlControls;


\n


\n

public partial class _Default : System.Web.UI.Page


\n

{


\n

protected void Page_Load(object sender, EventArgs e)


\n

{


\n

Label1.Text = Session["name"].ToString();


\n

/*对象的增加


\n

Application.Add(“App1″, “Value1″);


\n

Application.Add(“App2″, “Value2″);


\n

Application.Add(“App3″, “Value3″);


\n

Response.Write(“Application对象的使用”);


\n

Response.Write(“<br>”);


\n

for (int i = 0; i < Application.Count; i++)


\n

{


\n

Response.Write(“变量名:” + Application.GetKey(i));


\n

Response.Write(“,值:” + Application[i] + “<p>”);


\n

Response.Write(“<br>”);


\n

}*/


\n

}


\n

protected void Button2_Click(object sender, EventArgs e)


\n

{


\n

Application["content"] = TextBox1.Text;


\n

TextBox2.Text = TextBox2.Text + “\\n” + Label1.Text + “说:” + Application["content"].ToString();


\n

}


\n

}


\n


\n