469,957 Members | 2,705 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,957 developers. It's quick & easy.

Failed to load viewstate

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
1 5872
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.

Similar topics

3 posts views Thread by Eamonn O'Connell | last post: by
9 posts views Thread by John Kirksey | last post: by
6 posts views Thread by hitendra15 | last post: by
1 post views Thread by rockdale | last post: by
2 posts views Thread by Rotsey | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.