[英]how to keep one List with saved entries after 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。 在page_load之后,應該如何保存ID以供使用?
您必須將列表放入Viewstate
public IList<string> QuestionIDs
{
get
{
var obj = ViewState["QuestionIDs"];
if(obj == null)
{
obj = new List<string>();
ViewState["QuestionIDs"] = obj;
}
return (IList<string>)obj;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.