Failed to load viewstate. The control tree into which viewstate is..

I got this error message when i was working with dynamic control earlier. It seems this exception is thrown when some control in the control tree can not successfully load it’s viewstate.

Let’s take a look at the code,

1 var lvQuestions = new ListView(); 2 lvQuestions.ID = "lvQuestions"; 3 lvQuestions.LayoutTemplate = new QuestionsLayoutTemplate(); 4 lvQuestions.ItemTemplate = new QuestionsItemTemplate(); 5 lvQuestions.EditItemTemplate = new QuestionsEditTemplate(); 6 lvQuestions.InsertItemTemplate = new QuestionsInsertTemplate(); 7 lvQuestions.InsertItemPosition = InsertItemPosition.LastItem; 8 lvQuestions.ItemInserting += new EventHandler(lvQuestions_ItemInserting); 9 lvQuestions.ItemDataBound += new EventHandler(lvQuestions_ItemDataBound); 10 lvQuestions.ItemEditing += new EventHandler(lvQuestions_ItemEditing); 11 lvQuestions.ItemDeleting += new EventHandler(lvQuestions_ItemDeleting); 12 lvQuestions.ItemCanceling += new EventHandler(lvQuestions_ItemCanceling); 13 lvQuestions.ItemUpdating += new EventHandler(lvQuestions_ItemUpdating); 14 lvQuestions.ItemCommand += new EventHandler(listView_ItemCommand); 15

i created a dynamic list view, set the templates and add the listview to a placeholder control. So far everything works fine, i can add a data and it display it, delete also works but the problem arise when i tried to edit the item. Let’s take a look at the item template and edit template

1 private class QuestionsItemTemplate : ITemplate 2 { 3 4 #region ITemplate Members 5 6 public void InstantiateIn(Control container) 7 { 8 var tr = new TableRow(); 9 var td = new TableCell(); 10 var literal = new System.Web.UI.WebControls.Literal(); 11 literal.ID = "ltQuestion"; 12 td.Controls.Add(literal); 13 tr.Controls.Add(td); 14 15 td = new TableCell(); 16 spanTag = new HtmlGenericControl("span"); 17 spanTag.Attributes.Add("class", "ico edit"); 18 button = new LinkButton(); 19 button.ID = "btnEdit"; 20 button.CommandName = "Edit"; 21 button.Text = "Edit"; 22 button.CausesValidation = false; 23 spanTag.Controls.Add(button); 24 td.Controls.Add(spanTag); 25 tr.Controls.Add(td); 26 27 spanTag = new HtmlGenericControl("span"); 28 spanTag.Attributes.Add("class", "ico remove"); 29 td = new TableCell(); 30 button = new LinkButton(); 31 button.ID = "btnDelete"; 32 button.CommandName = "Delete"; 33 button.Text = "Delete"; 34 button.CausesValidation = false; 35 spanTag.Controls.Add(button); 36 td.Controls.Add(spanTag); 37 tr.Controls.Add(td); 38 39 container.Controls.Add(tr); 40 } 41 42 #endregion 43 } 44 45 private class QuestionsEditTemplate : ITemplate 46 { 47 48 #region ITemplate Members 49 50 public void InstantiateIn(Control container) 51 { 52 var tr = new TableRow(); 53 var td = new TableCell(); 54 var textField = new System.Web.UI.WebControls.TextBox(); 55 textField.ID = "txtQuestion"; 56 td.Controls.Add(textField); 57 58 var requiredValidator = new RequiredFieldValidator(); 59 requiredValidator.ID = "regValTxtQuestion"; 60 requiredValidator.ControlToValidate = textField.ID; 61 requiredValidator.Text = "*"; 62 requiredValidator.ErrorMessage = "Please fill the question"; 63 requiredValidator.ValidationGroup = "regValTxtQuestion_Edit"; 64 td.Controls.Add(requiredValidator); 65 tr.Controls.Add(td); 66 67 spanTag = new HtmlGenericControl("span"); 68 spanTag.Attributes.Add("class", "ico cancel"); 69 button = new LinkButton(); 70 button.ID = "btnCancel"; 71 button.CommandName = "Cancel"; 72 button.Text = "Cancel"; 73 button.CausesValidation = false; 74 spanTag.Controls.Add(button); 75 td.Controls.Add(spanTag); 76 tr.Controls.Add(td); 77 78 spanTag = new HtmlGenericControl("span"); 79 spanTag.Attributes.Add("class", "ico save"); 80 button = new LinkButton(); 81 button.ID = "btnUpdate"; 82 button.CommandName = "Update"; 83 button.Text = "Update"; 84 spanTag.Controls.Add(button); 85 td.Controls.Add(spanTag); 86 tr.Controls.Add(td); 87 88 container.Controls.Add(tr); 89 } 90 91 #endregion 92 } 93

It’s pretty straight forward, i have a literal control that display the value in view mode and a texbox control to display the value in edit mode. But everytime i try to edit it i got the same error again and again. And then a thought cross my mind.. what if i set all the controls in the item template and edit template but control their visibility property. Will that do the trick, let’s see..

1 private class QuestionsItemTemplate : ITemplate 2 { 3 4 #region ITemplate Members 5 6 public void InstantiateIn(Control container) 7 { 8 var tr = new TableRow(); 9 var td = new TableCell(); 10 var literal = new System.Web.UI.WebControls.Literal(); 11 literal.ID = "ltQuestion"; 12 td.Controls.Add(literal); 13 var textField = new System.Web.UI.WebControls.TextBox(); 14 textField.Visible = false; 15 textField.ID = "txtQuestion"; 16 td.Controls.Add(textField); 17 tr.Controls.Add(td); 18 19 td = new TableCell(); 20 spanTag = new HtmlGenericControl("span"); 21 spanTag.Attributes.Add("class", "ico edit"); 22 button = new LinkButton(); 23 button.ID = "btnEdit"; 24 button.CommandName = "Edit"; 25 button.Text = "Edit"; 26 button.CausesValidation = false; 27 spanTag.Controls.Add(button); 28 td.Controls.Add(spanTag); 29 30 spanTag = new HtmlGenericControl("span"); 31 spanTag.Attributes.Add("class", "ico cancel"); 32 spanTag.Style.Add("display", "none"); 33 button = new LinkButton(); 34 button.Visible = false; 35 button.ID = "btnCancel"; 36 button.CommandName = "Cancel"; 37 button.Text = "Cancel"; 38 button.CausesValidation = false; 39 spanTag.Controls.Add(button); 40 td.Controls.Add(spanTag); 41 tr.Controls.Add(td); 42 43 spanTag = new HtmlGenericControl("span"); 44 spanTag.Attributes.Add("class", "ico remove"); 45 td = new TableCell(); 46 button = new LinkButton(); 47 button.ID = "btnDelete"; 48 button.CommandName = "Delete"; 49 button.Text = "Delete"; 50 button.CausesValidation = false; 51 spanTag.Controls.Add(button); 52 td.Controls.Add(spanTag); 53 54 spanTag = new HtmlGenericControl("span"); 55 spanTag.Attributes.Add("class", "ico save"); 56 spanTag.Style.Add("display", "none"); 57 button = new LinkButton(); 58 button.Visible = false; 59 button.ID = "btnUpdate"; 60 button.CommandName = "Update"; 61 button.Text = "Update"; 62 spanTag.Controls.Add(button); 63 td.Controls.Add(spanTag); 64 tr.Controls.Add(td); 65 66 container.Controls.Add(tr); 67 } 68 69 #endregion 70 } 71

Above is the modified item template, i include all controls that are used in both item template and edit template and basically just handle their visibility property. The edit template has simillar contruct but with different controls being hidden, you get the idea. And so.. compile, run the code.. fingers crossed.. and it works!. It has the same control hierarchy when on view mode and edit mode but display different controls, i guess using dynamic controls can be quite tricky. Hope this helps.

Advertisements