将字典放入ViewState的问题

时间:2011-06-19 14:49:09

标签: c# asp.net viewstate

当我将字典放入ViewState时,我得到了这个。

Type 'System.Web.UI.WebControls.Table' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中的起源位置的更多信息。

Exception Details: System.Runtime.Serialization.SerializationException: Type 'System.Web.UI.WebControls.Table' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

我不明白,我是如何摆脱这种异常的...我知道我需要制作可序列化的东西......但是什么?

字典看起来像这样:

Dictionary<string, List<DisplayAllQuestionsTable>> tPages;

DisplayAllQuestionsTable中有一个以编程方式生成的表webcontrol

要序列化的类:

[Serializable()]
    public class DisplayAllQuestionsTable 
    {
        Table Tmain = new Table();
        TableRow threadRow;
        Label viewsLabel;
        Label repliesLabel;
        TableCell qTitle;
        TableCell qView;
        TableCell qReplies;
        TableCell qAvatar;
        TableCell moderatorDelete;
        HyperLink q;
        Label lastPostedBy;
        TableCell tCellLastPostedBy;
        Button btnMoveThread;
        Button btnDeleteThread;
        TableCell tCellmoderaterControls;
        Dictionary<string, string[]> allTopics;
        DropDownList drpDnTopics;
        DropDownList drpDnsubtopics;
        AllQuestions page;
        string Name { get; set; }
        string ThreadName { get; set; }
        string Topic { get; set; }
        string Subtopic { get; set; }
        int Views { get; set; }
        int Replies { get; set; }
        int PageNumber { get; set; }
        DateTime Time { get; set; }
        PlaceHolder holder2;
        public DisplayAllQuestionsTable(AllQuestions formPage, string name, string ThreadTitle, string topic, string subtopics, int views, int replies, int pageNumber, DateTime time, PlaceHolder pholder)
        {
            page = formPage;
            Name = name;
            ThreadName = ThreadTitle;
            Topic = topic;
            Subtopic = subtopics;
            Views = views;
            Replies = replies;
            PageNumber = pageNumber;
            DateTime Time = time;
            holder2 = pholder;
        }



        public void ExecuteAll()
        {
            CreateTable();

            feedInformation();
            CreateLabels();
            InitializeCells();
            AddControlsToCells();
            if (HttpContext.Current.User.IsInRole("Administrators") || HttpContext.Current.User.IsInRole("Moderators"))
            {

                AddModeratorControls();
                ManageDropDownList();
                initializeManagerControls();
                AddModeraterCells();
                ModeraterAddCellsToRows();
            }


            AddCellsToRows();

            if (HttpContext.Current.User.IsInRole("Administrators") || HttpContext.Current.User.IsInRole("Moderators"))
            {

                ModeraterAddCellsToRowsDelete();
            }
            CreateTable();
            holder2.Controls.Add(Tmain);
            // page.Form.Controls.Add(Tmain);
        }


        void feedInformation()
        {
            /*
             * Functions:
             * 1) SQLCommand= find all the question
             * 2) SQLCommnad= find the view per question
             * 3) SQLCommand= find the replies per thread
             * 4) SQLCommand= extract all Users avatars.
             * 5) Add functions to check the amount of questions. 
             * if too many questions swap pages (add a page button).
             * 6) The Moderator will have the authority to delete question
             * and move it to a different place. - New move buttons should be added!
             * 
             */
            q = new HyperLink();
            q.Text = ThreadName;
            q.NavigateUrl = "AnswerQuestion.aspx";
            allTopics = new Dictionary<string, string[]>();
            allTopics = AllQuestions.AllTopics();
        }

        void AddModeratorControls()
        {
            btnMoveThread = new Button();
            btnMoveThread.Text = "העבר ל";
            btnDeleteThread = new Button();
            btnDeleteThread.Text = "מחק";

            drpDnTopics = new DropDownList();
            drpDnsubtopics = new DropDownList();
            drpDnsubtopics.Width = 120;
            drpDnTopics.AutoPostBack = true;
            drpDnTopics.SelectedIndexChanged += topicDropDownMenu_SelectedIndexChanged;
        }

        void ManageDropDownList()
        {

            foreach (var item in allTopics)
            {
                drpDnTopics.Items.Add(item.Key);
            }
        }

        void topicDropDownMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            drpDnsubtopics.Items.Clear();
            string[] chosenItem = allTopics[drpDnTopics.SelectedItem.Value];

            foreach (string item in chosenItem)
            {
                drpDnsubtopics.Items.Add(item);
            }
        }

        void CreateLabels()
        {
            //Call everytime a question is inserted.
            viewsLabel = new Label();
            repliesLabel = new Label();
            lastPostedBy = new Label();

            viewsLabel.Text = "צפיות" + "<br/>" + Views;
            repliesLabel.Text = "תגובות" + "<br/>" + Replies;
            lastPostedBy.Text = "בעל אשכול" + "<br/>" + Name;
        }

        void initializeManagerControls()
        {
            tCellmoderaterControls = new TableCell();
            moderatorDelete = new TableCell();

            tCellmoderaterControls.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;
        }

        void InitializeCells()
        {
            //Initialize all controls
            threadRow = new TableRow();
            qTitle = new TableCell();
            qView = new TableCell();
            qReplies = new TableCell();
            qAvatar = new TableCell();
            tCellLastPostedBy = new TableCell();


            //Adding all controls
            qTitle.Width = 470;
            qTitle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;
            tCellLastPostedBy.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            qView.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            qReplies.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;

            qTitle.VerticalAlign = System.Web.UI.WebControls.VerticalAlign.Middle;
            tCellLastPostedBy.VerticalAlign = System.Web.UI.WebControls.VerticalAlign.Middle;
            qView.VerticalAlign = System.Web.UI.WebControls.VerticalAlign.Middle;
            qReplies.VerticalAlign = System.Web.UI.WebControls.VerticalAlign.Middle;
            qTitle.Controls.Add(q);
        }

        void AddControlsToCells()
        {
            //Call everytime a question in inserted.

            qReplies.Controls.Add(repliesLabel);
            qView.Controls.Add(viewsLabel);
            tCellLastPostedBy.Controls.Add(lastPostedBy);
        }

        void AddModeraterCells()
        {

            tCellmoderaterControls.Controls.Add(btnDeleteThread);
            tCellmoderaterControls.Controls.Add(drpDnsubtopics);
            tCellmoderaterControls.Controls.Add(drpDnTopics);
            tCellmoderaterControls.Controls.Add(btnMoveThread);
            moderatorDelete.Controls.Add(btnDeleteThread);
        }

        void ModeraterAddCellsToRows()
        {
            threadRow.Cells.Add(tCellmoderaterControls);

        }

        void AddCellsToRows()
        {
            //Call everytime a question is inserted

            threadRow.Cells.Add(qView);
            threadRow.Cells.Add(qReplies);
            threadRow.Cells.Add(tCellLastPostedBy);
            threadRow.Cells.Add(qAvatar);
            threadRow.Cells.Add(qTitle);
            Tmain.Rows.Add(threadRow);
        }

        void ModeraterAddCellsToRowsDelete()
        {


            threadRow.Cells.Add(moderatorDelete);
        }

        void CreateTable()
        {
            //Call only once
            Tmain.Width = 1000;
            Tmain.BorderColor = System.Drawing.Color.Black;
            Tmain.BorderWidth = 1;
            Tmain.CellPadding = 3;
    }

}

2 个答案:

答案 0 :(得分:2)

看起来你正在尝试序列化表控件而不是字典。你是否将字典数据绑定到表格控件?如果是这样,请尝试序列化字典而不是表控件,然后在页面加载时重新绑定。

答案 1 :(得分:0)

好像你试图将web控件而不是字典保存到viewstate中。有示例代码吗?