如何在page_load之后保留一个列表和保存的条目?

时间:2011-06-15 12:09:13

标签: c# asp.net list pageload

我有一个清单:

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 个答案:

答案 0 :(得分: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;
   }
}