Reputation: 73
I need to store some lists into a ViewState.
I created a class:
[Serializable]
public class TemplateTypeProcedureListViewState
{
public string Name { get; set; }
public string ProcedureName { get; set; }
public string ProcedureParameter { get; set; }
public string ProcedureParameterType { get; set; }
}
Here is the ViewState:
private TemplateTypeProcedureListViewState TemplateTypeProcedureList
{
get { return (TemplateTypeProcedureListViewState)ViewState["TemplateTypeProcedureList"]; }
set { ViewState["TemplateTypeProcedureList"] = value; }
}
I'm giving the values like this:
var templateTypeProcedureListViewState = new TemplateTypeProcedureListViewState
{
Name = ListNameTextBox.Text,
ProcedureName = ListProcedureNameTextBox.Text,
ProcedureParameter = ListProcedureParameterTextBox.Text,
ProcedureParameterType = ListProcedureParameterTypeTextBox.Text
};
ViewState.Add("TemplateTypeProcedureList", templateTypeProcedureListViewState);
Now, when I add another item, the first one is overwritten (or deleted and the other one (second) is saved). Is it possible to save more than one item into a ViewState? Can you refer me something or somewhere? Thanks :)
Upvotes: 0
Views: 590
Reputation: 4652
Ok, this happens because you save your new item in Viestate, you do not add an item to list saved in viewstate.
You have do retrieve list first, and only then add item, and of couse save it after.
var list = TemplateTypeProcedureList.Add(new item);
TemplateTypeProcedureList = list;
PS: It's not the best way to use viestate, as your page will become larger and larger in size. Try to avoid saving to much info on ViewState.
Upvotes: 2