ASP.NET内置对象二

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

(1)Respose对象

利用Response对象输出文字信息:

PRotected void Page_Load(object sender, EventArgs e)

{

string message = "您好,欢迎光临本网站!";

Response.Write(message);

}

利用Response对象实现网页跳转:

protected void Page_Load(object sender, EventArgs e)

{

Response.Redirect("http://www.easou.com");

}

利用response对象像浏览器输出字符串:

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(@"C:\Users\light\Desktop\Sample\Sample\Default.aspx");

}

利用response对象停止输出:

protected void Page_Load(object sender, EventArgs e)

{

for(int ss=1;ss<30;ss++)

{

Response.Writer(i);

if(i==8)

Response.End();

}

}

利用Response对象传递参数:

Response.Resirect("page.aspx?Num=4");

(2)Request对象

QueryString的使用。使用QueryString来获取从上一个页面传递来的字符串参数。

在Default.aspx中:

<div>

<a href="page1.aspx?Num=1&Name=Liu">页面跳转</a>

</div>

在page1.aspx.cs中:

protected void Page_Load(object sender, EventArgs e)

{

Response.Write("传递来的变量值:");

Response.Write("Num的值:" + Request.QueryString["Num"] + "Name的值" + Request.QueryString["Name"]);

}

用类似的方法也可以获取Form、Cookies、SeverVaiables的值。调用方法是:Request.Collection["variable"](variable是要查询的关键字)。Collection包括QueryString、Form、Cookies、SeverVaiables四种集合。使用方式也可以是Request["variable"],与Request.Collection["variable"]的效果一样。省略了Collection,Request对象就会依照QueryString,Form,Cookies,SeverVaiables的顺序查找,直到发现了variable所指的关键字并返回其值,否则方法返回空值。

Form集合:

Request.Form["Name"].ToString();

ServerVariable集合:

Request.ServerVariable[参数类型];

Cookies集合:

写入数据:

Response.Cookies[Cookie名称].Value=数据;

Response.Cookies[Cookie索引号]=数据;

读取数据:

CookiesValue=Request.Cookies[Cookie名称].Value;

CookiesValue=Request.Cookies[Cookie索引号].Value;

移出某项数据:

Response.Cookies.Remove("Cookie名称");

移出所有数据:

Response.Cookies.Clear();

Saves的使用(将HTTP请求保存到磁盘):

protected void Page_Load(object sender, EventArgs e)

{

Request.SaveAs("G:/sample.txt", true);

}

(3)Server对象

MachineName的使用。获取本地服务器计算机名称。

protected void Page_Load(object sender, EventArgs e)

{

string machineName;

machineName = Server.MachineName.ToString();

Response.Write(machineName);

}

HtmlEncode、HtmlDecode的使用。将<h1>HTML内容</h1>编码后输出到浏览器中,再利用HtmlDecode方法将编码后的结果还原。

protected void Page_Load(object sender, EventArgs e)

{

string str1;

str1 = Server.HtmlEncode("<h1>HTML编码</h1>");//编码

Response.Write(str1 + "<br/>" + "<br/>");

str1 = Server.HtmlDecode(str1);

Response.Write(str1);

}

URLEncode、UrlDecode的使用。Server对象的URLEncode方法根据URL规则对字符串进行编码。Server对象的UrlDecode方法根据URL规则对字符串进行解码。URL规则是当字符串数据以URL的形式传递到服务器时,在字符串中不允许出现空格,也不允许出现特殊字符。

protected void Page_Load(object sender, EventArgs e)

{

string str2;

str2 = Server.UrlEncode("http://www.easou.com");

Response.Write(str2+"<br/>"+"<br/>");

str2 = Server.UrlDecode(str2);

Response.Write(str2);

}

(4)ViewState对象

使用ViewState对象计数。

在Default.aspx中:

<div>

<a href="page1.aspx?Num=1&Name=Liu">页面跳转</a>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"/>

</div>

在Default.aspx.cs中:

protected void Button1_Click(object sender, EventArgs e)

{

int count ;

if (ViewState["count"] == null)

count = 1;

else

count = (int)ViewState["count"] + 1;

ViewState["count"] = count;

Response.Write("你单间按钮的次数为:" + ViewState["count"].ToString());

}

ViewState对象安全机制:

1.哈希编码技术。哈希编码技术被称为是一种强大的编码技术。它的算法思想是让asp.net检 查ViewState中的所有数据,然后通过散列算法把这些数据编码。该散列算法产生一段很短的数据 信息,即哈希代码。然后把这段代码加在ViewState信息后面。

哈希代码的的功能实际上是默认的,如果程序员希望有这样的功能,不需要采用额外的步骤, 但有时开发商选择禁用此项功能。以防止出现在一个网站系统中不同的服务器有不同的密钥。为了 禁用哈希代码,可以在Web.config文件中的<Pages>元素的enableViewStateMac属性。

<pages enableViewStateMac="false"/>

2.ViewState加密。尽管程序员使用了哈希代码,ViewState信息依然能够被用户阅读到,很多 情况下这是完全可以接受的。但如果ViewState里包含了需要保密的信息,就需要使用ViewState 加密。

设置单独某一页面使用ViewState加密:

<%Page ViewStateEncryptionMode="Always"%>

设置整个网站使用ViewState加密:

<pages viewStateEncryptionMode="Always"/>

使用ViewState对象保留成员变量。基本原理:在Page.PreRender事件发生时把成员变量保存在ViewState中,在Page.Load事件发生时取回成员变量。

在Default.aspx中:

<table>

<tr>

<td colspan="2">

<asp:TextBox ID="TextBox1" runat="server" Width="100px" BackColor="Beige" />

</td>

</tr>

<tr>

<td>

<asp:Button ID="Button1" runat="server" Width="50px" Text="保存信息" OnClick="Button1_Click"/>

</td>

<td>

<asp:Button ID="Button2" runat="server" Width="50px" Text="读取信息" OnClick="Button2_Click"/>

</td>

</tr>

</table>

在Default.aspx.cs中:

protected string TextBoxContent = "";

protected void Page_Load(object sender, EventArgs e)

{

if (this.IsPostBack)//回送

{

TextBoxContent = ViewState["TextBoxContent"].ToString();

}

}

protected void Page_PreRender(object sender, EventArgs e)

{

ViewState["TextBoxContent"] = TextBoxContent;

}

protected void Button1_Click(object sender, EventArgs e)

{

TextBoxContent = this.TextBox1.Text.ToString();//存储文本信息

this.TextBox1.Text = "";//清除信息

}

protected void Button2_Click(object sender, EventArgs e)

{

this.TextBox1.Text = TextBoxContent;//读取TextBoxContent信息

}

页面间信息传递:

1)跨页传递。

在Default.aspx中:

<div>

asp:TextBox ID="TextBox1" runat="server" Width="100px" BackColor="Beige" />

<br/>

<asp:Button ID="Button3" runat="server" Text="跨页传递" PostBackUrl="~/page1.aspx"/>

</div>

在Default.aspx.cs中:

public string FullContent

{

get

{

return this.Title.ToString() + " " + this.TextBox1.Text.ToString();

}

}

在page1.aspx.cs中:

protected void Page_Load(object sender, EventArgs e)

{

if (Page.PreviousPage != null)

Response.Write(Page.PreviousPage.Title.ToString());

Default default1=PreviousPage as Default ;

if (default1 != null)

Response.Write("<br/>" + default1.FullContent);//读取Default属性值

}

2)使用QueryString

在Default.aspx中:

<div>

<asp:Button ID="Button1" runat="server" Text="QueryString" OnClick="Button1_Click"/>

</div>

在Default.aspx.cs中:

protected void Button1_Click(object sender, EventArgs e)

{

Response.Redirect("page1.aspx?name=light&age=22");

}

在page1.aspx.cs中:

protected void Page_Load(object sender, EventArgs e)

{

Response.Write("传递过来的信息为:" + "<br>");

Response.Write("name:" + Request.QueryString["name"].ToString() + "<br>");

Response.Write("age:" + Request.QueryString["age"].ToString());

}

(5)Cookies对象

Cookie对象默认有效时间为20分钟,超过20分钟Cookie便会被清除。

写入数据:

Response.Cookies[Cookie名称].Value=数据;

Response.Cookies[Cookie索引号]=数据;

读取数据:

CookiesValue=Request.Cookies[Cookie名称].Value;

CookiesValue=Request.Cookies[Cookie索引号].Value;

移出某项数据:

Response.Cookies.Remove("Cookie名称");

移出所有数据:

Response.Cookies.Clear();

创建一个cookie实例:

HttpCookie cookie = new HttpCookie("test");//创建一个cookie实例

cookie.Values.Add("Name", "周周");//添加要存储的信息,采用键/值结合的方式

cookie.Expires = DateTime.Now.AddYears(1);

Response.Cookies.Add(cookie);//把cookie加入当前的页面的Respose对象里

获取cookie信息:

HttpCookie cookie2 = Request.Cookies["test"];

string name1;//声明一个变量用于存储cookie信息

if (cookie2 != null)//判断cookie是否为空

{

name1 = cookie2.Values["Name"];

Response.Write("<br><br>保存的用户名:" + name1);

}

修改cookie值:

int counter = 0;

if (Request.Cookies["counter"] == null)

{

counter = 0;

}

else

{

counter = counter + 1;

}

Response.Cookies["counter"].Value = counter.ToString();

Response.Cookies["counter"].Expires = System.DateTime.Now.AddDays(1);

删除一个cookie:

HttpCookie cookie3 = new HttpCookie("test");

cookie3.Expires = DateTime.Now.AddYears(-1);

Response.Cookies.Add(cookie3);

删除当前域中的所有cookie:

HttpCookie cookie4;

int limit = Response.Cookies.Count;

for (int i = 0; i < limit; i++)

{

cookie4= Request.Cookies[i];

cookie4.Expires = DateTime.Now.AddYears(-1);//设置过期

Response.Cookies.Add(cookie4);//覆盖

}

for (int i = 0; i < limit; i++)//判断cookie是否为空

{

cookie4 = Request.Cookies[i];

name = cookie4.Values["Name"];

Response.Write("<br><br>保存的用户名:" + name);

}

(6)session对象

读取数据:

数据=Session[变量名]或数据=Session[索引号]。

写入数据:

Session[变量名]=数据或Session[索引号]=数据。

移出Session对象中某项数据:

Session.Remove("命名对象");

Session.RemoveAt("命名对象索引");

移出Session对象中的所有数据:

Session.RemoveAll();

Session.Clear();

应用举例:

在Default.aspx中:

<div>

<table>

<tr>

<td colspan="2" style="height:80px">

<asp:Label ID="Label1" runat="server"/>

</td>

</tr>

<tr>

<td colspan="2">

<asp:label ID="Label2" runat="server" Text="请选择要查看的学生姓名:"/>

</td>

</tr>

<tr>

<td rowspan="2" style="width:200px">

<asp:ListBox ID="ListBox1" runat="server" Width="100px" Height="100px"/>

</td>

<td style="width:120px;height:20px">

<asp:Button ID="Button1" runat="server" Text="详细信息:" OnClick="Button1_Click"/>

</td>

</tr>

<tr>

<td style="width:120px;height:100px">

<asp:Label ID="Label3" runat="server"/>

</td>

</tr>

</table>

</div>

在Default.aspx.cs中:

public class Student

{

public string StuName;

public string StuAge;

public string StuScore;

public Student(string stuName, string stuAge, string stuScore)

{

StuName = stuName;

StuAge = stuAge;

StuScore = stuScore;

}

}

protected void Page_Load(object sender, EventArgs e)

{

if (!this.IsPostBack)

{

//定义Student对象

Student student1 = new Student("李米", "24", "89");

Student student2 = new Student("刘宇", "22", "95");

Student student3 = new Student("吴雅", "23", "86");

//Session存储studnet信息

Session["student1"] = student1;

Session["student2"] = student2;

Session["student3"] = student3;

//绑定数据到ListBox

this.ListBox1.Items.Clear();

this.ListBox1.Items.Add(student1.StuName);

this.ListBox1.Items.Add(student2.StuName);

this.ListBox1.Items.Add(student3.StuName);

}

//显示Session信息

this.Label1.Text = "SessionId:" + Session.SessionID.ToString() + "<br>";

this.Label1.Text += "Session数量:" + Session.Count.ToString() + "<br>";

this.Label1.Text += "Session模式:" + Session.Mode.ToString() + "<br>";

this.Label1.Text += "Session有效期:" + Session.Timeout.ToString();

}

protected void Button1_Click(object sender, EventArgs e)

{

if (this.ListBox1.SelectedIndex == -1)

this.Label1.Text = "";

else

{

//获取Session键值

string key = "student" + (this.ListBox1.SelectedIndex + 1).ToString();

//获取student对象

Student student = (Student)Session[key];

//显示学生信息

this.Label3.Text += "学生姓名:" + student.StuName + "<br>";

this.Label3.Text += "学生年龄:" + student.StuAge + "<br>";

this.Label3.Text += "学生成绩:" + student.StuScore;

}

}

Session存储。

1.在客户端存储。默认状态下在客户端使用Cookie存储Session信息。有时为了防止用户禁用Cookie造成程序混乱,不使用Cookie存储Session信息。

2.在服务器端存储。包括存储在进程内、存储在进程外、存储在SQL Server中。

(7)application对象

Application对象写入数据格式:

Application[变量名]=数据;

Application[索引号]=数据。

Application对象读取数据格式:

数据=Application[变量名];

数据=Application[索引号]。

删除Application对象中的某项数据:

Application.Remove("命名对象");

Application.RemoveAt("命名对象的索引");

移出Application对象中的所有数据:

Application.RemoveAll();

Application.Clear();

加锁:Application.Lock();解锁:Application.UnLock();使用时必须成对出现。

使用实例:

在Global.asax.cs中:

protected void Application_Start(object sender, EventArgs e)

{

//应用程序启动时运行的代码

Application["count"] = 0;

}

protected void Session_Start(object sender, EventArgs e)

{

//新会话启动时运行的代码

Application.Lock();

int count = Convert.ToInt32(Application["count"]);

Application["count"] = count + 1;

Application.UnLock();

}

在Default.aspx.cs中:

protected void Page_Load(object sender, EventArgs e)

{

//第一次加载页面

if (!IsPostBack)

{

string Count = Application["count"].ToString();

Response.Write("访问次数为:" + Count + "次");

}

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航