ASP.NET頁面間傳值的幾種方式

來源:互聯網
上載者:User

文章來自網上,僅供學習之用,著作權歸實際作者所有!

1。使用QueryString
      使用QuerySting在頁面間傳遞值已經是一種很老的機制了,這種方法的主要優點是實現起來非常簡單,然而它的缺點是傳遞的值是會顯示在瀏覽器的地址欄上的(不安全),同時又不能傳遞對象,但是在傳遞的值少而安全性要求不高的情況下,這個方法還是一個不錯的方案。使用這種方法的步驟如下:
1,使用控制項建立web表單(form)
2,建立可以返回表單的按鈕和連結按鈕
3,在按鈕或連結按鈕的單擊事件裡建立一個儲存URL的字元變數
4,在儲存的URL裡添加QueryString參數
5,使用Response.Redirect重新導向到上面儲存的URL
下面的代碼片斷示範了如何?這個方法:
  源頁面WebForm1.aspx.cs中的部分代碼:
private void Button1_Click(object sender, System.EventArgs e)
{
     string url;
     url="WebForm2.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text;
     Response.Redirect(url);
}
 目標頁面WebForm2.aspx.cs中的部分代碼:
private void Page_Load(object sender, System.EventArgs e)
{
     Label1.Text=Request.QueryString["name"];
     Label2.Text=Request.QueryString["email"];
}

2。使用Session變數
      使用Session變數是可以在頁面間傳遞值的的另一種方式,在本例中我們把控制項中的值存在Session變數中,然後在另一個頁面中使用它,以不同頁面間實現值傳遞的目的。但是,需要注意的是在Session變數儲存過多的資料會消耗比較多的伺服器資源,在使用session時應該謹慎,當然了,我們也應該使用一些清理動作來去除一些不需要的session來降低資源的無謂消耗。使用Session變數傳遞值的一般步驟如下:
1,在頁面裡添加必要的控制項
2,建立可以返回表單的按鈕和連結按鈕
3,在按鈕或連結按鈕的單擊事件裡,把控制項的值添加到session變數裡
4,使用Response.Redirect(或Server.Transfer)方法重新導向到另一個頁面
5,在另一個頁面提取session的值,在確定不需要使用該session時,要顯式清除它
下面的代碼片斷示範了如何?這個方法:
   源頁面WebForm1.aspx.cs中的部分代碼:
private void Button1_Click(object sender, System.EventArgs e)
{
     //textbox1 and textbox2 are webform
     //controls
     Session["name"]=TextBox1.Text;
     Session["email"]=TextBox2.Text;
     Server.Transfer("WebForm2.aspx");
}
  目標頁面WebForm2.aspx.cs中的部分代碼:
private void Page_Load(object sender, System.EventArgs e)
{
     Label1.Text=Session["name"].ToString();
     Label2.Text=Session["email"].ToString();
     Session.Remove("name");
     Session.Remove("email");
}

3.使用Server.Transfer
      這個方法相比上面介紹的方法稍微複雜一點,但在頁面間值傳遞中卻是特別有用的,使用該方法你可以在另一個頁面以對象屬性的方式來存取顯露的值,當然了,使用這種方法,你需要額外寫一些代碼以建立一些屬性以便可以在另一個頁面訪問它,但是,這個方式帶來的好處也是顯而易見的。總體來說,使用這種方法是簡潔的同時又是物件導向的。使用這種方法的整個過程如下:
1,在頁面裡添加必要的控制項
2,建立傳回值的Get屬性過程
3,建立可以返回表單的按鈕和連結按鈕
4,在按鈕單擊事件處理常式中調用Server.Transfer方法轉移到指定的頁面
5,在第二個頁面中,我們就可以使用Context.Handler屬性來獲得前一個頁面執行個體對象的引用,通過它,就可以使用存取前一個頁面的控制項的值了
以下代碼綜合實現上述步驟過程的代碼:
  源頁面WebForm1.aspx.cs中的部分代碼:
    把以下的代碼添加到頁面中
public string Name
{
     get
     {
         return TextBox1.Text;
     }
}

public string EMail
{
     get
     {
         return TextBox2.Text;
     }
}
  然後調用Server.Transfer方法
private void Button1_Click(object sender, System.EventArgs e)
{
     Server.Transfer("WebForm2.aspx");
}
   目標頁面代碼:

在WebForm2.aspx中務必在第一句話添加

<%@ Reference Page="~/WebForm1.aspx" %>或

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

然後在WebForm2.aspx.cs中添加如下。

private void Page_Load(object sender, System.EventArgs e)
{
     //create instance of source web form
     WebForm1 wf1;
     //get reference to current handler instance
     wf1=(WebForm1)Context.Handler;
     Label1.Text=wf1.Name;
     Label2.Text=wf1.EMail;
}

如果在調試的過程中遇到錯誤.就到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files中把建立的網站名的檔案夾刪掉.(要先關掉Visual Studio開發環境再刪)

4.使用@PreviousPageType指令

      這個指令是.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 />";

 5.利用某些控制項的PostBackUrl屬性

樣本:仍然是源頁面WebForm1.aspx和目標頁面WebForm2.aspx.

WebForm1.aspx中的部分代碼:

<asp:Button ID="btnPostBack" Runat="server" Text="PBButton"></asp:Button>

<asp:TextBox ID="txtName" Runat="server" ></asp:TextBox>

<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>

WebForm2.aspx.cs中的部分代碼:

protected void Page_Load(object Sender,System.EventArgs e)

{

    TextBox txtName;

    Calendar calendar1;

    txtName=(TextBox)PreviousPage.FindControl("txtName");

    calendar1=(Calendar)PreviousPage.FindControl("Calendar1");

    Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();

}

使用這種方法存在一個問題:如果在沒有單擊那個按鈕之前,也就是未處理WebForm1.aspx之前,有人請求了WebForm2.aspx,該怎麼辦?這就需要在WebForm2.aspx中的代碼處理之前加一個判斷.使用IsCrossPagePostBack屬性,這與IsPostBack 屬性很相似,它允許檢查請求是否來自WebForm1.aspx.如下:

protected void Page_Load(object Sender,System.EventArgs e)

{

    if(PreviousPage.IsCrossPagePostBack)

    {

        TextBox txtName;

        Calendar calendar1;

        txtName=(TextBox)PreviousPage.FindControl("txtName");

        calendar1=(Calendar)PreviousPage.FindControl("Calendar1");

        Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();

    }

    else

    {

        Response.Redirect("WebForm1.aspx");

    }

}

6.  使用Cookie物件變數
  這個也是大家常使用的方法,與Session一樣,是對每一個使用者而言的,但是有個本質的區別,即Cookie是存放在用戶端的,而session是存放在伺服器端的。而且Cookie的使用要配合ASP.NET內建對象Request來使用。

a.aspx的C#代碼
private void Button1_Click(object sender, System.EventArgs e)
{
    HttpCookie cookie_name = new HttpCookie("name");
    cookie_name.Value = Label1.Text;
    Reponse.AppendCookie(cookie_name);
    Server.Transfer("b.aspx");
}

b.aspx中C#代碼
private void Page_Load(object sender, EventArgs e)
{
    string name;
    name = Request.Cookie["name"].Value.ToString();
}

7.  使用Application 物件變數
  Application對象的作用範圍是整個全域,也就是說對所有使用者都有效。其常用的方法用Lock和UnLock。
a.aspx的C#代碼
private void Button1_Click(object sender, System.EventArgs e)
{
    Application["name"] = Label1.Text;
    Server.Transfer("b.aspx");
}

b.aspx中C#代碼
private void Page_Load(object sender, EventArgs e)
{
    string name;
    Application.Lock();
    name = Application["name"].ToString();
    Application.UnLock();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.