(论坛答疑点滴)怎么在ViewState中保存和取出自己定义的类
举例如下:
比如你的类
[Serializable]
class user
{
public string name;
public int age;
}
第一按钮是保存操作
user user1=new user();
user1.name="zhuye";
user1.age=23;
user user2=new user();
user2.name="chengli";
user2.age=21;
ArrayList al=new ArrayList();
al.Add(user1);
al.Add(user2);
ViewState["data"]=al;
第二个按钮是读取操作
ArrayList al=(ArrayList)ViewState["data"];
foreach(user o in al)
{
Response.Write(o.name+" "+o.age+"<br>");
}
比如你的类
[Serializable]
class user
{
public string name;
public int age;
}
第一按钮是保存操作
user user1=new user();
user1.name="zhuye";
user1.age=23;
user user2=new user();
user2.name="chengli";
user2.age=21;
ArrayList al=new ArrayList();
al.Add(user1);
al.Add(user2);
ViewState["data"]=al;
第二个按钮是读取操作
ArrayList al=(ArrayList)ViewState["data"];
foreach(user o in al)
{
Response.Write(o.name+" "+o.age+"<br>");
}
欢迎大家阅读我的极客时间专栏《Java业务开发常见错误100例》【全面避坑+最佳实践=健壮代码】
![](https://img2020.cnblogs.com/blog/103060/202004/103060-20200402094642952-518549746.jpg)