将字典放入 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;
}
}
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];
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在尝试序列化表控件而不是字典。您是否将字典数据绑定到表控件?如果是这样,请尝试序列化字典而不是表控件,然后在页面加载时重新绑定。
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.
似乎您正在尝试将 Web 控件而不是字典保存到视图状态中。有示例代码吗?
Seems like you're trying to save a web control, not a dictionary, into the viewstate. Got sample code?