ASP.NET内置对象二
(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 + "次");
- }
- }
ASP.NET内置对象二的更多相关文章
- 初识 Asp.Net内置对象之Response对象
Response对象 Respose对象用于将数据从服务器发送回浏览器.它允许将数据作为请求的结果发送到浏览器,并提供有光响应的信息,可以用来在页面中输入数据,在页面中跳转,还可以传递各个页面的参数, ...
- Unit05: JavaScript对象概述 、 常用内置对象一 、 常用内置对象二 、 常用内置对象三
Unit05: JavaScript对象概述 . 常用内置对象一 . 常用内置对象二 . 常用内置对象三 常用内置对象使用演示: <!DOCTYPE html> <html> ...
- Asp.net内置对象用途说明
Asp.net 内置对象 1.Session当客户第一次请求网页,session创建.当客户最后一次请求页面,一段时间后,session销毁.默认30分钟. 一般存用户信息,即登陆成功后,在sessi ...
- ASP.NET 内置对象涉略
一.ASP.NET中内置的常用对象的介绍 本文列举了ASP.NET 的八个内置对象,其中前五个是比较常用的. 1.Response Response 对象用于从服务器向用户发送输出的结果. Write ...
- 【ASP.NET 基础】ASP.NET内置对象
准确地说,asp.net 并没有内置对象这一说,jsp 里确实把 request.response 这些当作 jsp 的内置对象,这里只不过是借用了一下 jsp 的说法而已.在 Web 中处于中心的是 ...
- ASP.NET内置对象详解
ASP.NET的内置对象介绍 1.Response 2.Request 3.Server 4.Application 5.Session 6.Cookie Request对象主要是让服务器取得客户端浏 ...
- ASP.NET内置对象一
ASP.NET提供了大量的对象类库,在这些类库中包含了许多封装好的内置对象,我们只需要直接使用这些对象的方法和属性,就能简单快速地完成很多的功能.Request对象.Response对象和Serve对 ...
- 初识 Asp.Net内置对象之Server对象
Server对象 Server对象定义了一个于Web服务器相关联的类提供对服务器上的方法和属性的访问,用于访问服务器上的资源. Server对象的常用属性 属性 MarhineName 获取服务器 ...
- ASP.NET内置对象
ASP.NET中有六个内置对象 Response:向客户端输出信息或设置客户端输出状态. Request:获取客户端信息. Server:访问服务器的方法和属性. Application:用于将信息保 ...
随机推荐
- 版本控制、SVN、VSS
ylbtech-Miscellaneos: 版本控制.SVN.VSS 1.A,版本控制返回顶部 1, 版本控制(Revision control)是一种软体工程技巧,籍以在开发的过程中,确保由不同人所 ...
- jQuery图片延迟加载插件jQuery.lazyload
插件描述:jQuery图片延迟加载插件jQuery.lazyload,使用延迟加载在可提高网页下载速度.在某些情况下,它也能帮助减轻服务器负载. 使用方法 引用jquery和jquery.lazy ...
- gerrit 使用笔记
添加git hooks git库的钩子目录中有一个commit-msg脚本文件,可以在git执行commit时,在提交信息中自动添加一个唯一的Change-Id scp -P 29419 admin@ ...
- NSPredicate,谓词
原文地址:http://blog.csdn.net/holydancer/article/details/7380799 在语言上,谓语,谓词是用来判断的,比如“我是程序猿”中的是,就是表判断的谓语, ...
- Git中的文件状态和使用
(暂存区 即Index In Git) commit 到 local respository的内容,不想push,则使用git reset 将文件状态回转到staged|modified|unstag ...
- Java中的JDBC基础
简介 JAVA程序想要对数据库进行访问,需要有JDBC驱动程序的支持.JDBC驱动程序提供了对各种主流数据库的接口,程序员只需要学习掌握这一套接口,就可以实现对所有数据库的访问代码编写. 一般步骤 J ...
- HDU 4405 【概率dp】
题意: 飞行棋,从0出发要求到n或者大于n的步数的期望.每一步可以投一下筛子,前进相应的步数,筛子是常见的6面筛子. 但是有些地方可以从a飞到大于a的b,并且保证每个a只能对应一个b,而且可以连续飞, ...
- 剑指offer习题集
1.重载赋值运算符函数:(具体见代码) //普通做法 CMyString& CMyString::operator=(const CMyString& str) { if (this ...
- EDS 14.0 dtc:commmand not found
EDS 14.0在生成dtb文件时,输入命令: dtc -I dts -O dtb -o soc_system.dtb soc_system.dts 出现错误: bash:dtc:command on ...
- 【转】关系映射文件***.hbm.xml详解
http://blog.sina.com.cn/s/blog_7ffb8dd5010144yo.html 附.Oracle使用标准.可变长度的内部格式来存储数字.这个内部格式精度可以高达38位. NU ...