开发者

Problem with putting a dictionary into ViewState

I get this when I put a dictionary into a 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.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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.

What I dont understand, is how do I get rid of that exception...I know that i need to make something serializable..but what?

The dictionary looks like that:

Dictionary<string, List<DisplayAllQuestionsTable>> tPages;

DisplayAllQuestionsTable has got a table webcontrol in it that is generated programmatically

Class to serialize:

[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];

    开发者_Python百科        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;
    }

}


Looks like you're trying to serialize a table control rather than a dictionary. Are you databinding the dictionary to the table control? If so, try serializing the dictionary rather than the table control and then rebinding on page load.


Seems like you're trying to save a web control, not a dictionary, into the viewstate. Got sample code?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜