ASP.NET - List 泛型物件存到 Viewstate

List 泛型物件要存到 Viewstate 前,物件要設為可序列化,例如類別宣告時加上[Serializable],再把List泛型轉為 Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[Serializable]
public class Clist {
public int No;
public string[] People;
public Clist () {
No = 0;
People = new string[4] {"0", "0", "0", "0"};
}
}

public List<Clist> VSList
{
get {
//return (List<cvList>)ViewState["cvList"];
if ( !( ViewState["VSList"] is List<Clist> ) )
{
// need to fix the memory and added to viewstate
ViewState["VSList"] = new List<Clist>();
}
return (List<Clist>)ViewState["VSList"];
}
set { ViewState["VSList"] = value; }
}

Clist myClass = new Clist();
myClass.No = 1;
myClass.People[0] = "ReactJS";
myClass.People[1] = "AngularJS";
myClass.People[2] = "NodeJS";
myClass.People[3] = "VueJS";
VSList.Add(myClass);

參考資料
geekswithblogs

0%