2011-06-15 69 views
0

我有一個列表:如何在page_load之後保存一個具有保存條目的列表?

public List<string> QuestionIDs; 

構造:

public EmployeeCategorizationControl() 
     { 
      QuestionIDs = new List<string>(); 
     } 

很多單選按鈕這樣的:

<asp:RadioButtonList ID="selectedYesNo1" runat="server" RepeatDirection="Horizontal" 
       OnSelectedIndexChanged="FirstQuestionGotAnswered"> 
       <asp:ListItem Text="Yes" Value="1"></asp:ListItem> 
       <asp:ListItem Text="No" Value="0"></asp:ListItem> 
      </asp:RadioButtonList> 

我試圖挽救QuestionID在此列出:

protected void FirstQuestionGotAnswered(object sender, EventArgs e) 
     { 

      QuestionID = selectedYesNo1.Text; 
      QuestionIDs.Add(QuestionID); 

}

我的問題是,頁面加載後,當我根據以前的ID調用下一個問題時,根本沒有列表中的任何ID。我如何保存id以便在page_load之後使用它們?

回答

1

你必須把在ViewState列表

public IList<string> QuestionIDs 
{ 
    get 
    { 
     var obj = ViewState["QuestionIDs"]; 
     if(obj == null) 
     { 
     obj = new List<string>(); 
     ViewState["QuestionIDs"] = obj; 
     } 
     return (IList<string>)obj; 
    } 
} 
+0

schould我也刪除QuestionIDs =新名單的初始化();從構造函數? – 2011-06-15 12:17:17

+1

是的,如果您使用我答案中的代碼,那麼列表將在需要時在屬性內部創建。 – Magnus 2011-06-15 12:20:03

相關問題