zoukankan      html  css  js  c++  java
  • ASP.NET页面传值不使用QueryString

    ASP.NET页面传值不使用QueryString

     

    Asp.net中的页面传值方法:

    1         Url传值

    特点:主要优点是实现起来非常简单,然而它的缺点是传递的值是会显示在浏览器的地址栏上的(不安全),同时又不能传递对象,

    使用场景:传递的值少而安全性要求不高的情况下

    传递:url="WebForm2.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text;
    接收:name=   Request.QueryString["name"];
            email=Request.QueryString["email"];

    2         Session

    特点:Session变量存放在服务器端,存储过多的数据会消耗比较多的服务器资源,可是使用清理动作来去除一些不需要的session,Session容易过期

    使用场景:三个以上页面需要共用的变量

    传递:Session["name"]=TextBox1.Text;

    接收:  name=Session["email"].ToString();
    清除: Session.Remove("name");
    3         Cookie

    特点:Cookie是存放在客户端的,需要要配合ASP.NET内置对象Request来使用

    传递:HttpCookie cookie_name = new HttpCookie("name");
        cookie_name.Value = Label1.Text;
        Reponse.AppendCookie(cookie_name);
    接收:name = Request.Cookie["name"].Value.ToString();

    4         Application

    特点:Application对象的作用范围是整个全局,对所有用户都有效。其常用的方法用Lock和UnLock

    传递:Application["name"] = Label1.Text;
    接收:    Application.Lock();
                name = Application["name"].ToString();
                Application.UnLock();

    5         Response.Redirect()

    传递:Response.Redirect( "target.aspx?param1=1111&param2=2222 ")
    接收:   string   str   =   Request[ "param1 "]

    6       Server.Transfer(子页面特点:地址栏中没有地址,鼠标右键属性中显示父页面的路径,保护子页面Url安全)

    传递:Server.Transfer( "target.aspx?param1=1111&param2=2222 ")
    接收:string   str   =   Request[ "param1 "]
    接收:

    a.aspx的C#代码

    复制代码
    publicstring Name
    {
    get{ return Label1.Text;}
    }
    privatevoid Button1_Click(object sender, System.EventArgs e)
    {
    Server.Transfer(
    "b.aspx");
    }
    复制代码

    b.aspx中C#代码

    privatevoid Page_Load(object sender, EventArgs e)
    {
    a newWeb
    = (a)Context.Handler;
    string name;
    name
    = newWeb.Name;
    }

    7 表单提交(可隐藏Url中的参数)

    <form   action=   "target.aspx "   method   =   "post "   name   =   "form1 ">
      <input   name   =   "param1 "   value   =   "1111 "/>
      <input   name   =   "param2 "   value   =   "2222 "/>  
          </form>
          ....
          form1.submit();
          ....
          此种方在ASP。NET中无效,因为ASP。NET的表单总是提交到自身页面,如果要提交到别一页面,需要特殊处理。

    8  @PreviousPageType (可隐藏Url中的参数)

    这个指令是.net 2.0中的一个新指令,用于处理ASP.NET 2.0提供的跨页面传送新功能.用于批定跨页面的传送过程起始于哪个页面.包含两个属性:

    TypeName:设置回送时的派生类名

    VirtualPath:设置回送时所传送页面的地址.

    如下示例:

    源页面WebForm1.aspx中有一个TextBox,ID为txtName.在WebForm1.aspx.cs中设置一个属性:

    public TextBox Name

    {

        get{return this.txtName;}//返回一个控件对象

    }

    在目标页面的设计文件中(WebForm2.aspx)的最上方加上:

     <%@ PreviousPageType VirtualPath="~/Page1.aspx"%>,

    然后就能引用WebForm1.aspx中定义的属性了.

    在WebForm2.aspx.cs中可以有如下引用形式(假设WebForm2.aspx中有一个ID为lblName的Label):

    lblName.Text="Hello"+PreviousPage.Name.Text+"<br />";

    9     postbackUrl (可隐藏Url中的参数)

    首先在asp.net中只有Button linkButton imgButton有postbackUrl属性可以用来跨页传递控件的值

    <asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="index.aspx" />

    Index.aspx:

      if(Page.PreviousPage!=null)

         {

            if(Page.PreviousPage.IsCrossPagePostBack)

            {

                TextBox textControl=this.PreviousPage.FindControl("TextBox1") as TextBox;

     

                if(textControl.Text!=null)

                 {

                     this.Label1.Text = textControl.Text;

                 }

             }

        }

       PreviousPage:代表传递值的页面

    IsCrossPagePostBack:判断页面是不是跨页传值

    FindControl:获得控件的值

  • 相关阅读:
    NOIP2017 D1T2 时间复杂度
    NOIP2017 游记
    NOIP2017 Day-1 模板荟萃
    NOIP2013 货车运输 倍增
    洛谷3933 Chtholly Nota Seniorious 二分答案+贪心
    洛谷2474 [SCOI2008] 天平 差分约束->枚举
    bzoj1270 BeijingWc2008 雷涛的小猫 DP
    poj1061--青蛙的约会--扩展欧几里得
    “整除”的相关
    poj1067--取石子游戏--(神奇的黄金分割比)
  • 原文地址:https://www.cnblogs.com/914556495wxkj/p/3754905.html
Copyright © 2011-2022 走看看