• 微软原版系统

  • 一键重装系统

  • 纯净系统

  • 在线技术客服

魔法猪系统重装大师 一键在线制作启动 U 盘 PE 系统 用一键重装的魔法拯救失去灵魂的系统
当前位置:首页 > 教程 > 电脑教程

Asp.net页面几种传值的方法分享

时间:2015年04月02日 15:24:23    来源:魔法猪系统重装大师官网    人气:17076

1、利用网页Cookie传值

下面为起始页Defaut1.aspx.cs部分代码:

protected void Button1_Click1(object sender, EventArgs e)
{
       HttpCookie cookie_name = new HttpCookie("myname");
       cookie_name.Value = this.TextBox1.Text;
       Response.AppendCookie(cookie_name);
       //Server.Transfer("ShowerInfo.aspx");
    Response.Redirect("ShowerInfo.aspx");
}

下面接受传值页面ShowerInfo.aspx.cs代码:

protected void Page_Load(object sender, EventArgs e)
{
        this.Label1.Text = Request.Cookies["myname"].Value.ToString();
}

2、利用@ PreviousPageType指令和实体类实现页面传值

PreviousPageType指令是ASP.NET 2.0跨页面回送机制的一部分,允许指定来源页面的虚拟路径,以便强类型访问来源页面。正常情况下,发送的数据可通过PreviousPage属性和FindControl方法访问,但使用强类型的PreviousPageType指令允许访问源页面上的公共属性,而不需要调用FindControl方法。
     如有下面一简单userInfo类: 

public class UserInfo
{
    public UserInfo()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
      
        private TextBox textName;
        public TextBox TextName
        {
            get
            {
                return textName;
            }

            set
            {
                textName = value;
            }
        }

        private TextBox textPwd;
        public TextBox TextPwd
        {
            get
            {
                return textPwd;
            }

            set
            {
                textPwd = value;
            }
        }

        private TextBox textEmail;
        public TextBox TextEmail
        {
            get
            {
                return textEmail;
            }

            set
            {
                textEmail = value;
            }
        }
}

起始页面Default1.aspx文件代码:



    无标题页


    

起始页面Default1.aspx.cs代码:

public partial class Default1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
   
    public UserInfo myInfo    //为页面添加一UserInfo类型的属性。从而实现传递复杂数据。
    {
        get
        {
            UserInfo info = new UserInfo();
            info.TextName = TextName;
            info.TextPwd = TextPwd;
            info.TextEmail = TextEmail;
            return info;
        }
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        this.Submit.PostBackUrl = "~/ShowerInfo.aspx";

        //上面的跳转语句不可以用如:Response.Redirect(“ShowerInfo.aspx”)来替换,否则会出现:“未将对象引用设置到对象的实例。”错误。
    }
}


下面为接受页面ShowerInfo.aspx部分代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
  <%@ PreviousPageType VirtualPath="~/Default1.aspx" %>




    无标题页


    




上面一行被标注为红色的代码不可少:<%@ PreviousPageType VirtualPath="~/Default1.aspx" %> ,从而可以使得接受页面ShowerInfo.aspx.cs中可以利用
UserInfo mInfo = PreviousPage.myInfo 语句获取上一页面Default1.aspx设置的公共属性myInfo的值。

下面为接受页面ShowerInfo.aspx.cs部分代码:

public partial class ShowerInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UserInfo mInfo = PreviousPage.myInfo;
        Label1.Text = mInfo.TextName.Text;
        Label2.Text = mInfo.TextPwd.Text;
        Label3.Text = mInfo.TextEmail.Text;

    }
}

3、利用Server.Transfer( )和Context.Handler实现页面传值
   起始页面Default1.aspx.cs代码:

public partial class Default1 : System.Web.UI.Page
{
    private string name;
    private string pwd;

    public String Name
    {
        get
        {
            return this.TextBox1.Text;
        }

    }

    public string Pwd
    {
        get
        {
            return this.TextBox2.Text;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {


    }
       
    protected void Button1_Click1(object sender, EventArgs e)
    {
        Server.Transfer("ResutlPage.aspx");
        
    }
}

上面代码中只能使用Server.Transfer()方法实现页面的跳转。Web 窗体页是应用程序中的类,因此可以向处理任何类一样为它们创建属性。Server.Transfer只能够转跳到本地虚拟目录指定的页面,并且跳到目标页面后,浏览器显示的地址并不会改变(这种特性有时可能是有益处的!),这样就在极其有限的页面生存周期内,通过特别技术:Context.Handler让目标页面能读取源页面的属性值。

接受传值页面ResutlPage.aspx.cs代码:

protected void Page_Load(object sender, EventArgs e)
{
       
        if (!IsPostBack)
        { 
            
            Default1  myver;
            myver = (Default1) Context.Handler;
            this.Label1.Text=myver.Name;
            this.Label2.Text = myver.Pwd;
        }
}

Asp.net,页面,几种,传值,的,方法,分享,、,利用,
栏目:电脑教程 阅读:1000 2023/12/27
Win7教程 更多>>
U盘教程 更多>>
Win10教程 更多>>
魔法猪学院 更多>>

Copyright © 2015-2023 魔法猪 魔法猪系统重装大师

本站发布的系统仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件。

在线客服 查看微信 返回顶部