欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET内置对象之Application对象

程序员文章站 2022-05-05 16:02:11
新建一个网站,包括两个网页,代码如下: 1、index.aspx代码: 复制代码 代码如下:<%@ page language="c#" autoevent...
新建一个网站,包括两个网页,代码如下:
1、index.aspx代码: 
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="index.aspx.cs" inherits="index" %>
<!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>
<table style="width: 300px; height: 100px">
<tr>
<td style="width: 100px">
用户名:</td>
<td style="width: 246px">
<asp:textbox id="textbox1" runat="server"></asp:textbox></td>
</tr>
<tr>
<td style="width: 100px">
密    码:</td>
<td style="width: 246px">
<asp:textbox id="textbox2" runat="server" textmode="password"></asp:textbox></td>
</tr>
<tr>
<td style="text-align: center;" colspan="2">
<asp:button id="button1" runat="server" text="登录" onclick="button1_click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>

index.aspx.cs代码: 
复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.collections;
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 index : system.web.ui.page
{
string strinfo;
protected void page_load(object sender, eventargs e)
{
}
protected void button1_click(object sender, eventargs e)
{
application["info"] = textbox1.text;
strinfo = application["info"].tostring();
if (textbox1.text == "admin" && textbox2.text == "admin")
{
session["name"] = textbox1.text;
response.redirect("default.aspx?info=" + strinfo + "");//地址栏的传值
}
}
}
2、default.aspx代码:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<!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>
<table style="width: 500px; height: 323px">
<tr>
<td colspan="2">
<asp:textbox id="textbox2" runat="server" height="314px" width="497px" textmode="multiline"></asp:textbox></td>
</tr>
<tr>
<td>
<asp:label id="label1" runat="server" text="label"></asp:label>
<asp:textbox id="textbox1" runat="server" width="390px"></asp:textbox></td>
<td>
 
<asp:button id="button2" runat="server" text="发送" onclick="button2_click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
default.aspx.cs代码:
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)
{
label1.text = session["name"].tostring();
/*对象的增加
application.add("app1", "value1");
application.add("app2", "value2");
application.add("app3", "value3");
response.write("application对象的使用");
response.write("<br>");
for (int i = 0; i < application.count; i++)
{
response.write("变量名:" + application.getkey(i));
response.write(",值:" + application[i] + "<p>");
response.write("<br>");
}*/
}
protected void button2_click(object sender, eventargs e)
{
application["content"] = textbox1.text;
textbox2.text = textbox2.text + "\n" + label1.text + "说:" + application["content"].tostring();
}
}