asp.net页面几种方式_Asp.net页面几种传值的方法分享

2009-08-23 ASP.NET 阅读:

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文件代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      
        <asp:TextBox ID="TextName" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextPwd" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextEmail" runat="server"></asp:TextBox>
        <asp:Button ID="Submit" runat="server" onclick="Submit_Click" Text="传值" />
        <br />
    
    </div>
    </form>
</body>
</html>

起始页面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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
    
    </div>
    </form>
</body>
</html>

上面一行被标注为红色的代码不可少:<%@ 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页面几种方式_Asp.net页面几种传值的方法分享

http://m.quanqiunao.cn/bianchengkaifa/14378/

推荐访问:

ASP.NET推荐文章

推荐内容

上一篇:【已使用】使用Glimpse 监测ASP.NET MVC网站性能 下一篇:asp.net中使用jquery ajax_asp.net中使用jquery上传组件Uploadify进行多文件上传实例教程