(1)Respose对象

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

  1. protected void Page_Load(object sender, EventArgs e)
    {
  2.   string message = "您好,欢迎光临本网站!";
  3.   Response.Write(message);
  4. }

  

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

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. Response.Redirect("http://www.easou.com");
  4. }

  

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

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. Response.Write(@"C:\Users\light\Desktop\Sample\Sample\Default.aspx");
  4. }

  利用response对象停止输出:

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. for(int ss=1;ss<30;ss++)
  4. {
  5. Response.Writer(i);
  6. if(i==8)
  7. Response.End();
  8. }
  9. }

  利用Response对象传递参数:

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

(2)Request对象

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

    在Default.aspx中:

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

    

    在page1.aspx.cs中:

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. Response.Write("传递来的变量值:");
  4. Response.Write("Num的值:" + Request.QueryString["Num"] + "Name的值" + Request.QueryString["Name"]);
  5. }

  

  用类似的方法也可以获取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请求保存到磁盘):

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. Request.SaveAs("G:/sample.txt", true);
  4. }

 (3)Server对象

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

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. string machineName;
  4. machineName = Server.MachineName.ToString();
  5. Response.Write(machineName);
  6. }

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

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. string str1;
  4. str1 = Server.HtmlEncode("<h1>HTML编码</h1>");//编码
  5. Response.Write(str1 + "<br/>" + "<br/>");
  6. str1 = Server.HtmlDecode(str1);
  7. Response.Write(str1);
  8. }

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

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. string str2;
  4. str2 = Server.UrlEncode("http://www.easou.com");
  5. Response.Write(str2+"<br/>"+"<br/>");
  6. str2 = Server.UrlDecode(str2);
  7. Response.Write(str2);
  8. }

(4)ViewState对象

  使用ViewState对象计数。

    在Default.aspx中:

  1. <div>
  2. <a href="page1.aspx?Num=1&Name=Liu">页面跳转</a>
  3. <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"/>
  4. </div>

    在Default.aspx.cs中:

  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. int count ;
  4. if (ViewState["count"] == null)
  5. count = 1;
  6. else
  7. count = (int)ViewState["count"] + 1;
  8. ViewState["count"] = count;
  9. Response.Write("你单间按钮的次数为:" + ViewState["count"].ToString());
  10. }

  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中:

  1. <table>
  2.   <tr>
  3.     <td colspan="2">
  4.       <asp:TextBox ID="TextBox1" runat="server" Width="100px" BackColor="Beige" />
  5.     </td>
  6.   </tr>
  7.   <tr>
  8.     <td>
  9.       <asp:Button ID="Button1" runat="server" Width="50px" Text="保存信息" OnClick="Button1_Click"/>
  10.     </td>
  11.     <td>
  12.       <asp:Button ID="Button2" runat="server" Width="50px" Text="读取信息" OnClick="Button2_Click"/>
  13.     </td>
  14.   </tr>
  15. </table>

    在Default.aspx.cs中:

  1. protected string TextBoxContent = "";
  2. protected void Page_Load(object sender, EventArgs e)
  3. {
  4. if (this.IsPostBack)//回送
  5. {
  6. TextBoxContent = ViewState["TextBoxContent"].ToString();
  7. }
  8. }
  9.  
  10. protected void Page_PreRender(object sender, EventArgs e)
  11. {
  12. ViewState["TextBoxContent"] = TextBoxContent;
  13. }
  14.  
  15. protected void Button1_Click(object sender, EventArgs e)
  16. {
  17. TextBoxContent = this.TextBox1.Text.ToString();//存储文本信息
  18. this.TextBox1.Text = "";//清除信息
  19. }
  20.  
  21. protected void Button2_Click(object sender, EventArgs e)
  22. {
  23. this.TextBox1.Text = TextBoxContent;//读取TextBoxContent信息
  24. }

  页面间信息传递:

    1)跨页传递。

      在Default.aspx中:

  1. <div>
  2. asp:TextBox ID="TextBox1" runat="server" Width="100px" BackColor="Beige" />
  3. <br/>
  4. <asp:Button ID="Button3" runat="server" Text="跨页传递" PostBackUrl="~/page1.aspx"/>
  5. </div>

      在Default.aspx.cs中:

  1. public string FullContent
  2. {
  3. get
  4. {
  5. return this.Title.ToString() + " " + this.TextBox1.Text.ToString();
  6. }
  7. }

      在page1.aspx.cs中:

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (Page.PreviousPage != null)
  4. Response.Write(Page.PreviousPage.Title.ToString());
  5. Default default1=PreviousPage as Default ;
  6. if (default1 != null)
  7. Response.Write("<br/>" + default1.FullContent);//读取Default属性值
  8. }

    2)使用QueryString

      在Default.aspx中:

  1. <div>
  2. <asp:Button ID="Button1" runat="server" Text="QueryString" OnClick="Button1_Click"/>
  3. </div>

      在Default.aspx.cs中:

  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. Response.Redirect("page1.aspx?name=light&age=22");
  4. }

      在page1.aspx.cs中:

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. Response.Write("传递过来的信息为:" + "<br>");
  4. Response.Write("name:" + Request.QueryString["name"].ToString() + "<br>");
  5. Response.Write("age:" + Request.QueryString["age"].ToString());
  6. }

(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实例:

  1. HttpCookie cookie = new HttpCookie("test");//创建一个cookie实例
  2. cookie.Values.Add("Name", "周周");//添加要存储的信息,采用键/值结合的方式
  3. cookie.Expires = DateTime.Now.AddYears(1);
  4. Response.Cookies.Add(cookie);//把cookie加入当前的页面的Respose对象里

  获取cookie信息:

  1. HttpCookie cookie2 = Request.Cookies["test"];
  2. string name1;//声明一个变量用于存储cookie信息
  3. if (cookie2 != null)//判断cookie是否为空
  4. {
  5. name1 = cookie2.Values["Name"];
  6. Response.Write("<br><br>保存的用户名:" + name1);
  7. }

  修改cookie值:

  1. int counter = 0;
  2. if (Request.Cookies["counter"] == null)
  3. {
  4. counter = 0;
  5. }
  6. else
  7. {
  8. counter = counter + 1;
  9. }
  10. Response.Cookies["counter"].Value = counter.ToString();
  11. Response.Cookies["counter"].Expires = System.DateTime.Now.AddDays(1);

  删除一个cookie:

  1. HttpCookie cookie3 = new HttpCookie("test");
  2. cookie3.Expires = DateTime.Now.AddYears(-1);
  3. Response.Cookies.Add(cookie3);

  删除当前域中的所有cookie:

  1. HttpCookie cookie4;
  2. int limit = Response.Cookies.Count;
  3. for (int i = 0; i < limit; i++)
  4. {
  5. cookie4= Request.Cookies[i];
  6. cookie4.Expires = DateTime.Now.AddYears(-1);//设置过期
  7.     Response.Cookies.Add(cookie4);//覆盖
  8.    }
  9. for (int i = 0; i < limit; i++)//判断cookie是否为空
  10.    {
  11. cookie4 = Request.Cookies[i];
  12. name = cookie4.Values["Name"];
  13. Response.Write("<br><br>保存的用户名:" + name);
  14. }

(6)Session对象

  读取数据:

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

  写入数据:

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

  移出Session对象中某项数据:

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

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

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

    Session.RemoveAll();

    Session.Clear();

  应用举例:

    在Default.aspx中:

  1. <div>
  2. <table>
  3. <tr>
  4. <td colspan="2" style="height:80px">
  5. <asp:Label ID="Label1" runat="server"/>
  6. </td>
  7. </tr>
  8. <tr>
  9. <td colspan="2">
  10. <asp:label ID="Label2" runat="server" Text="请选择要查看的学生姓名:"/>
  11. </td>
  12. </tr>
  13. <tr>
  14. <td rowspan="2" style="width:200px">
  15. <asp:ListBox ID="ListBox1" runat="server" Width="100px" Height="100px"/>
  16. </td>
  17. <td style="width:120px;height:20px">
  18. <asp:Button ID="Button1" runat="server" Text="详细信息:" OnClick="Button1_Click"/>
  19. </td>
  20. </tr>
  21. <tr>
  22. <td style="width:120px;height:100px">
  23. <asp:Label ID="Label3" runat="server"/>
  24. </td>
  25. </tr>
  26. </table>
  27. </div>

    在Default.aspx.cs中:

  1. public class Student
  2. {
  3. public string StuName;
  4. public string StuAge;
  5. public string StuScore;
  6. public Student(string stuName, string stuAge, string stuScore)
  7. {
  8. StuName = stuName;
  9. StuAge = stuAge;
  10. StuScore = stuScore;
  11. }
  12. }
  13.  
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. if (!this.IsPostBack)
  17. {
  18. //定义Student对象
  19. Student student1 = new Student("李米", "24", "89");
  20. Student student2 = new Student("刘宇", "22", "95");
  21. Student student3 = new Student("吴雅", "23", "86");
  22. //Session存储studnet信息
  23. Session["student1"] = student1;
  24. Session["student2"] = student2;
  25. Session["student3"] = student3;
  26. //绑定数据到ListBox
  27. this.ListBox1.Items.Clear();
  28. this.ListBox1.Items.Add(student1.StuName);
  29. this.ListBox1.Items.Add(student2.StuName);
  30. this.ListBox1.Items.Add(student3.StuName);
  31. }
  32. //显示Session信息
  33. this.Label1.Text = "SessionId:" + Session.SessionID.ToString() + "<br>";
  34. this.Label1.Text += "Session数量:" + Session.Count.ToString() + "<br>";
  35. this.Label1.Text += "Session模式:" + Session.Mode.ToString() + "<br>";
  36. this.Label1.Text += "Session有效期:" + Session.Timeout.ToString();
  37. }
  38.  
  39. protected void Button1_Click(object sender, EventArgs e)
  40. {
  41. if (this.ListBox1.SelectedIndex == -1)
  42. this.Label1.Text = "";
  43. else
  44. {
  45. //获取Session键值
  46. string key = "student" + (this.ListBox1.SelectedIndex + 1).ToString();
  47. //获取student对象
  48. Student student = (Student)Session[key];
  49. //显示学生信息
  50. this.Label3.Text += "学生姓名:" + student.StuName + "<br>";
  51. this.Label3.Text += "学生年龄:" + student.StuAge + "<br>";
  52. this.Label3.Text += "学生成绩:" + student.StuScore;
  53. }
  54. }

  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中:

  1. protected void Application_Start(object sender, EventArgs e)
  2. {
  3. //应用程序启动时运行的代码
  4. Application["count"] = 0;
  5. }
  6.  
  7. protected void Session_Start(object sender, EventArgs e)
  8. {
  9. //新会话启动时运行的代码
  10. Application.Lock();
  11. int count = Convert.ToInt32(Application["count"]);
  12. Application["count"] = count + 1;
  13. Application.UnLock();
  14. }

    在Default.aspx.cs中:

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. //第一次加载页面
  4. if (!IsPostBack)
  5. {
  6. string Count = Application["count"].ToString();
  7. Response.Write("访问次数为:" + Count + "次");
  8. }
  9. }

ASP.NET内置对象二的更多相关文章

  1. 初识 Asp.Net内置对象之Response对象

    Response对象 Respose对象用于将数据从服务器发送回浏览器.它允许将数据作为请求的结果发送到浏览器,并提供有光响应的信息,可以用来在页面中输入数据,在页面中跳转,还可以传递各个页面的参数, ...

  2. Unit05: JavaScript对象概述 、 常用内置对象一 、 常用内置对象二 、 常用内置对象三

    Unit05: JavaScript对象概述 . 常用内置对象一 . 常用内置对象二 . 常用内置对象三 常用内置对象使用演示: <!DOCTYPE html> <html> ...

  3. Asp.net内置对象用途说明

    Asp.net 内置对象 1.Session当客户第一次请求网页,session创建.当客户最后一次请求页面,一段时间后,session销毁.默认30分钟. 一般存用户信息,即登陆成功后,在sessi ...

  4. ASP.NET 内置对象涉略

    一.ASP.NET中内置的常用对象的介绍 本文列举了ASP.NET 的八个内置对象,其中前五个是比较常用的. 1.Response Response 对象用于从服务器向用户发送输出的结果. Write ...

  5. 【ASP.NET 基础】ASP.NET内置对象

    准确地说,asp.net 并没有内置对象这一说,jsp 里确实把 request.response 这些当作 jsp 的内置对象,这里只不过是借用了一下 jsp 的说法而已.在 Web 中处于中心的是 ...

  6. ASP.NET内置对象详解

    ASP.NET的内置对象介绍 1.Response 2.Request 3.Server 4.Application 5.Session 6.Cookie Request对象主要是让服务器取得客户端浏 ...

  7. ASP.NET内置对象一

    ASP.NET提供了大量的对象类库,在这些类库中包含了许多封装好的内置对象,我们只需要直接使用这些对象的方法和属性,就能简单快速地完成很多的功能.Request对象.Response对象和Serve对 ...

  8. 初识 Asp.Net内置对象之Server对象

    Server对象 Server对象定义了一个于Web服务器相关联的类提供对服务器上的方法和属性的访问,用于访问服务器上的资源. Server对象的常用属性 属性   MarhineName 获取服务器 ...

  9. ASP.NET内置对象

    ASP.NET中有六个内置对象 Response:向客户端输出信息或设置客户端输出状态. Request:获取客户端信息. Server:访问服务器的方法和属性. Application:用于将信息保 ...

随机推荐

  1. 版本控制、SVN、VSS

    ylbtech-Miscellaneos: 版本控制.SVN.VSS 1.A,版本控制返回顶部 1, 版本控制(Revision control)是一种软体工程技巧,籍以在开发的过程中,确保由不同人所 ...

  2. jQuery图片延迟加载插件jQuery.lazyload

      插件描述:jQuery图片延迟加载插件jQuery.lazyload,使用延迟加载在可提高网页下载速度.在某些情况下,它也能帮助减轻服务器负载. 使用方法 引用jquery和jquery.lazy ...

  3. gerrit 使用笔记

    添加git hooks git库的钩子目录中有一个commit-msg脚本文件,可以在git执行commit时,在提交信息中自动添加一个唯一的Change-Id scp -P 29419 admin@ ...

  4. NSPredicate,谓词

    原文地址:http://blog.csdn.net/holydancer/article/details/7380799 在语言上,谓语,谓词是用来判断的,比如“我是程序猿”中的是,就是表判断的谓语, ...

  5. Git中的文件状态和使用

    (暂存区 即Index In Git) commit 到 local respository的内容,不想push,则使用git reset 将文件状态回转到staged|modified|unstag ...

  6. Java中的JDBC基础

    简介 JAVA程序想要对数据库进行访问,需要有JDBC驱动程序的支持.JDBC驱动程序提供了对各种主流数据库的接口,程序员只需要学习掌握这一套接口,就可以实现对所有数据库的访问代码编写. 一般步骤 J ...

  7. HDU 4405 【概率dp】

    题意: 飞行棋,从0出发要求到n或者大于n的步数的期望.每一步可以投一下筛子,前进相应的步数,筛子是常见的6面筛子. 但是有些地方可以从a飞到大于a的b,并且保证每个a只能对应一个b,而且可以连续飞, ...

  8. 剑指offer习题集

    1.重载赋值运算符函数:(具体见代码) //普通做法 CMyString& CMyString::operator=(const CMyString& str) { if (this ...

  9. 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 ...

  10. 【转】关系映射文件***.hbm.xml详解

    http://blog.sina.com.cn/s/blog_7ffb8dd5010144yo.html 附.Oracle使用标准.可变长度的内部格式来存储数字.这个内部格式精度可以高达38位. NU ...