By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,780 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,780 IT Pros & Developers. It's quick & easy.

Failed to load viewstate

P: n/a
et
I. Failed to load viewstate. The control tree into which
viewstate is being loaded must match the control tree that was used to save
viewstate during the previous request. For example, when adding controls
dynamically, the controls added during a post-back must match the type and
position of the controls added during the initial request.

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.Web.HttpException: Failed to load viewstate. The
control tree into which viewstate is being loaded must match the control
tree that was used to save viewstate during the previous request. For
example, when adding controls dynamically, the controls added during a
post-back must match the type and position of the controls added during the
initial request.

Source Error:

Line 70: 'do we want to clear controls???

Line 71: me.ph.Controls.clear

Line 72: me.ph.Controls.Add(me.oCtrlDemo)

Line 73: End Sub

Line 74:


On my default page, I have a treeview and when the user clicks an item, it
selects a control to load into a placeholder. I get this error after a
couple of clicks around, what am I doing wrong, I am clearing the controls,
and I even tried loading only if not postback, then tried reloading every
time... This is my code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

'if not ispostback then

me.ph.Controls.clear

DoTheLoad()

'end if

End Sub

Public Sub tvwMenu_SelectedIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tvwMenu1.SelectedIndexChanged

intSelectedMenu = e.newnode.tostring

me.ph.Controls.clear

DoTheLoad(intSelectedMenu)

End Sub

Private Sub DoTheLoad(Optional i as string = "")

select case i

Case "0.0"

me.oCtrlDemo = LoadControl("Controls\ctl1.ascx")

Case "0.2"

me.oCtrlDemo = LoadControl("ctl2.ascx")

Case else

me.oCtrlDemo = LoadControl("Controls\ctlDefault.ascx")
End Select

me.ph.Controls.clear

me.ph.Controls.Add(me.oCtrlDemo)

End Sub
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,
Do you need ViewState enabled? If not try one of theese:
1. Set the ph object EnableViewState property to False
2. Set the DOCUMENT EnableViewState property to False

You need ViewState enabled if you change control properties like font, color
etc. (but not, for example, Text on a TextBox) in code and need to persist
them through post-backs. Otherwise, you may disable ViewState and it has the
added benefit of reducing payload sizes - especially if you have DataGrids.

"et" wrote:
I. Failed to load viewstate. The control tree into which
viewstate is being loaded must match the control tree that was used to save
viewstate during the previous request. For example, when adding controls
dynamically, the controls added during a post-back must match the type and
position of the controls added during the initial request.

...

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.