Failed to load viewstate

E

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top