我收到“在程序集中键入'ViewStateTest._Default + sInfo'时没有标记为可序列化”在回发后尝试保留通用列表值时出错,我在这里做错了什么?
代码:
public partial class _Default : System.Web.UI.Page
{
List<sInfo> InfoList1 = new List<sInfo>();
sInfo Info1;
struct sInfo
{
public int LSR;
public string BrandAcc;
public string CreateDate;
public string QName;
}
private void CreateArray()
{
Info1.LSR = 2;
Info1.BrandAcc = "AA";
Info1.CreateDate = "12/12/2011";
Info1.QName = "Completed";
InfoList1.Add(Info1);
}
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["arrayListInViewState"] != null)
{
InfoList1 = (List<sInfo>)ViewState["arrayListInViewState"];
}
else
{
// ArrayList isn't in view state, so we need to load it from scratch.
CreateArray();
}
// Code that uses PageArrayList.
Label1.Text = InfoList1[0].LSR.ToString();
Label2.Text = InfoList1[0].BrandAcc;
Label3.Text = InfoList1[0].CreateDate;
Label4.Text = InfoList1[0].QName;
}
void Page_PreRender(object sender, EventArgs e)
{
// Save PageArrayList before the page is rendered.
ViewState.Add("arrayListInViewState", InfoList1);
}
protected void Timer1_Tick(object sender, EventArgs e)
{
}
html代码:
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick"></asp:Timer>
<asp:HiddenField ID="hfTableTopInfoCount" runat="server" Value="0" />
<div id="Container">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
答案 0 :(得分:3)
[Serializable]
struct sInfo
进入viewstate的对象需要标记为可序列化。