2011-02-07 43 views
3

我試圖將張貼的數據從一頁傳遞到下一頁,但上一頁總是等於空。PreviousPage =空的跨頁張貼

這是我在原來的頁面

<asp:Button ID="mmGo" runat="server" Text="Merge" PostBackUrl="~/Default2.aspx?action=merge"/> 

然後在下面的網頁我有(一個UpdatePanel 內):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> 
<%@ PreviousPageType VirtualPath="~/Default.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> 
    </div> 
    </form> 
</body> 
</html> 

...並在cs文件:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (PreviousPage != null) 
    { 
     string action = Request.QueryString["action"]; 
     if (action == "merge") 
     { 
      UpdatePanel SourceControl = (UpdatePanel)PreviousPage.FindControl("UpdatePanel1"); 
      HiddenField SourceTextBox = (HiddenField)SourceControl.FindControl("txtListIDs"); 
      if (SourceTextBox != null) 
      { 
       Label1.Text = SourceTextBox.Value; 
      } 
     } 
    } 
    else 
    { 
     Label1.Text = "page is not cross page post back!"; 
    } 
} 

任何想法爲什麼PreviousPage總是等於null?

回答

5

PreviousPage property返回使用Server.Transfer向此頁面發送控件的頁面。

如果由於直接請求(而不是從另一個頁面傳輸或交叉發佈)呈現當前頁面,則PreviousPage屬性包含null。

+0

因此,在按鈕上使用PostBackURL屬性不是傳輸或交叉郵件嗎? – baked 2011-02-07 14:43:55

2

是的...它的工作...當你使用按鈕或linkbutton,使用postbackurl,不要使用response.redirect。當您使用PostBackUrl屬性,你可以有控制,控制上10頁...參考觀看此影片,,一項PostBackUrl和Response.Redirect的之間

http://www.youtube.com/watch?v=P1AqBtOYOMI

區別?

PostBackURL將所有表單數據發送到指定頁面。而使用Response.Redirect發送表單數據到當前頁面,然後將用戶移動到指定頁面