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;
        }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM