日期:2014-05-18  浏览次数:20452 次

未标记为可序列化 错误,请高手帮助
我需要用StateView保存上次的值,但是,运行就报错,如果去掉StateView就不会有错误。
在网上搜索了说加一个什么[Serializable],但是加了也是完全没有用。
不知道哪里出现了问题,望大家指教。
C# code
[Serializable]
public partial class _Default : System.Web.UI.Page 
{
    
    RadioButton[] radYes ;
    RadioButton[] radNo ;
    public static int num = 0;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            radYes =new RadioButton[47];
            radNo = new RadioButton[47];
            displaySource();
            
           
        }
        else
        {


            radYes = (RadioButton[])ViewState["radYes"];
        }
    }

    public void displaySource()
    {
        StreamReader sr = new StreamReader(Server.MapPath("~/source.txt"), Encoding.Default);
        TableRow tr;
        TableCell tc;
       
       
        int count = 0;
      
        while (!sr.EndOfStream)
        {
            count++;
            string txt = sr.ReadLine();
            string[] content = txt.Split('@');

            tr= new TableRow();
           
            tc = new TableCell();
            tc.Text = content[0]; 
            tr.Cells.Add(tc);

            radYes[count - 1] = new RadioButton();
            radYes[count-1].Text = "是";
            radYes[count - 1].ID = "yes" + count;
            radYes[count - 1].Attributes.Add("value", content[1]);
            radYes[count - 1].GroupName = "e" + count;
            tc = new TableCell();
            tc.Controls.Add(radYes[count - 1]);
            tr.Cells.Add(tc);


            radNo[count - 1] = new RadioButton();
            radNo[count - 1].Text = "不是   ";
            radNo[count - 1].ID = "no" + count;
            radNo[count - 1].Attributes.Add("value", content[1]);
            radNo[count - 1].GroupName = "e" + count;
            tc = new TableCell();
            tc.Controls.Add(radNo[count - 1]);
            tr.Cells.Add(tc);
            
            tblDisplay.Rows.Add(tr);
            
           
           
        }
        num = count;
        ViewState["radYes"] = radYes;
        
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //ViewState["radYes"] = counter;
        int[] type = new int[9];
        for (int i = 0; i < radYes.Length; i++)
        {

            if (radYes[i].Checked)
            {
                int res = int.Parse(radYes[i].ID.Substring(3, 1));
                type[res - 1]++;
            }
           
           
        }
        for (int i = 0; i < type.Length; i++)
        {
            Response.Write((i+1)+"号性格: "+type[i].ToString()+"<br/>");
        }
    }
}




------解决方案--------------------
RadioButton不可序列化,并不是你在webpage里加个Serializable就可以解决,你应把RadioButton中相关的值保存下来,而不是保存RadioButton控件。
------解决方案--------------------
public void displaySource()
改成
public RadioButton[] displaySource() 返回RadioButton[]类型,不用ViewState了
------解决方案--------------------
楼上正解.....