• 页面之间9种数据传递


      1 Session
      2 Application
      3 QueryString
      4 Cache    前4个传递方法一样
      5 Server.Transfer
      6 HttpConetext
      7 Cookie
      8 文件传输
      9 数据库
     10 
     11 using System;
     12 using System.Collections.Generic;
     13 using System.IO;
     14 using System.Linq;
     15 using System.Web;
     16 using System.Web.UI;
     17 using System.Web.UI.WebControls;
     18 namespace WebApp
     19 {
     20     public partial class WebForm2 : System.Web.UI.Page
     21     {
     22         protected void Page_Load(object sender, EventArgs e)
     23         {
     24         }
     25 
     26         internal int Addparm { get; set; }
     27 
     28         protected void Button1_Click(object sender, EventArgs e)
     29         {
     30             Addparm = 1;
     31             Server.Transfer("WebForm1.aspx");
     32         }
     33 
     34         protected void Button2_Click(object sender, EventArgs e)
     35         {
     36             Context.Items["Id"] = 2;
     37             Server.Transfer("WebForm1.aspx");
     38         }
     39 
     40         protected void Button4_Click(object sender, EventArgs e)
     41         {
     42             Response.Cookies.Add(new HttpCookie("Id", "3"));
     43             Response.Redirect("WebForm1.aspx");
     44         }
     45 
     46         protected void Button5_Click(object sender, EventArgs e)
     47         {
     48             Cache["Id"] = 4;
     49             Response.Redirect("WebForm1.aspx");
     50         }
     51 
     52         protected void Button3_Click(object sender, EventArgs e)
     53         {
     54             using (var _writer = new StreamWriter(Server.MapPath("a.txt")))
     55             {
     56                 _writer.Write("5");
     57                 _writer.Close();
     58             }
     59             Response.Redirect("WebForm1.aspx");
     60         }
     61     }
     62 }
     63 
     64 取数据:
     65 
     66 using System;
     67 using System.Collections.Generic;
     68 using System.IO;
     69 using System.Linq;
     70 using System.Web;
     71 using System.Web.UI;
     72 using System.Web.UI.WebControls;
     73 
     74 namespace WebApp
     75 {
     76     public partial class WebForm1 : System.Web.UI.Page
     77     {
     78         protected void Page_Load(object sender, EventArgs e)
     79         {
     80             /*
     81             // Services.Transfer
     82             var id = ((WebForm2) PreviousPage).Addparm;
     83             */
     84 
     85             /*
     86             //HttpContext
     87             var id = Context.Items["Id"];
     88             */
     89 
     90             /*
     91             //Cookies
     92             var id = Request.Cookies["Id"].Value;
     93             */
     94 
     95             /*
     96             //Cache
     97             var id = Cache["Id"];
     98              */
     99             string id = null;
    100             using (var _reader = new StreamReader(Server.MapPath("a.txt")))
    101             {
    102                 id = _reader.ReadToEnd();
    103                 _reader.Close();
    104             }
    105             Response.Write(id);
    106         }
    107     }
    108 }
  • 相关阅读:
    开启Spring Boot 之旅
    Java笔试面试练习题---集合
    Python
    Python
    Redis -下载与基本使用
    Git
    Vue全家桶-Vue-router&Vuex
    Es6
    Vue-前端
    Django基础及实战
  • 原文地址:https://www.cnblogs.com/Liaoyang/p/3114777.html
Copyright © 2020-2023  润新知