2013-02-19 24 views
2

我有兩個頁面。 first.aspx和second.aspx。爲了得到first.aspx所有的控制值,我添加指令second.aspx即使在ASP.NET中,頁面加載後私有變量的值也不會保留

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

我沒有問題,讓以前所有的頁面控件並將其設置爲標籤,但我有一個很大的問題將這些值保存到私有變量,並在頁面加載事件完成後重新使用它。這裏是代碼示例。當我嘗試從另一個方法的輸入中獲取值時,它沒有添加任何內容。爲什麼?

public partial class Second : System.Web.UI.Page 
     {   
      List<string> input = new List<string>(); 
      protected void Page_Load(object sender, EventArgs e) 
      { 
        if (Page.PreviousPage != null&&PreviousPage.IsCrossPagePostBack == true) 
        { 
         TextBox SourceTextBox11 (TextBox)Page.PreviousPage.FindControl("TextBox11"); 
         if (SourceTextBox11 != null) 
         { 
          Label1.Text = SourceTextBox11.Text; 
          input.Add(SourceTextBox11.Text); 
         } 
        } 
       } 

      protected void SubmitBT_Click(object sender, EventArgs e) 
     { 
        //do sth with input list<string> 
        //input has nothing in it here. 
     } 
     } 

回答

0

SubmitBT_Click -click事件發生在回發中。但所有變量(和控件)都放置在頁面生命週期的末尾。所以你需要一種方法來保持你的List,例如在ViewStateSession

public List<String> Input 
{ 
    get 
    { 
     if (Session["Input"] == null) 
     { 
      Session["Input"] = new List<String>(); 
     } 
     return (List<String>)Session["Input"]; 
    } 
    set { Session["Input"] = value; } 
} 

Nine Options for Managing Persistent User State in Your ASP.NET Application

+0

感謝蒂姆。現在我明白了問題所在。 – John 2013-02-19 21:58:21

相關問題