Failed to load viewstate

Discussion in 'ASP .Net Web Controls' started by R Reyes, Oct 19, 2005.

  1. R Reyes

    R Reyes Guest

    Hi,

    The question I'm asking revolves around what is needed to load a viewstate.

    1) I am using Regular Expression Validators to validate text fields on a page.
    --Let's say I have a form set up. After I type in "bad data" in a date
    field and click submit, the validators will show up correctly with the errors
    I programmed in red saying bad data was entered.
    --Now, when I click on submit again (or even the back button) after
    correcting the dates, I get the "Failed to load viewstate" error.

    I've tried setting EnableViewState to both true and false but they don't
    make a difference. Tutorials online are not helpful either as I think they
    go into too much explanation about what a viewstate is, rather than how to
    debug this common problem that I can't seem to get past.

    I've coded a decent sized application for modifying Employee data and I
    can't get passed this error. I'm not about to start from scratch
    either...Can someone explain to me how I can stop this from happening?

    Here is the stack trace (which I am also not very good at figuring out):
    [HttpException (0x80004005): 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.]
    System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +343
    System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +142
    System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +142
    System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +142
    System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +142
    System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    System.Web.UI.Page.LoadAllState() +527
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3940

    Please help and post any comments as to what I'm doing wrong. Thanks in
    advance.
     
    R Reyes, Oct 19, 2005
    #1
    1. Advertising

  2. R Reyes

    R Reyes Guest

    I have 3 pages currently:
    Directory.aspx (list of employees)
    Profile.aspx (page for employee view/edit)
    Update.aspx (saving changes)

    Directory: This page uses a simple Datagrid bound to the recordset of
    active employees. There are no errors here and no other controls created
    besides the DataGrid.

    ---Directory.aspx->Profile.aspx...
    Profile, part 1: This page uses a DataList. In ItemTemplate, the bound data
    is read-only because you would have clicked on a particular employee from
    Directory.aspx to view their information.

    The only important tags/controls that are present in Profile.aspx are these:
    1. EmployeeID: input type="hidden" name="EmployeeID"
    value="<%#DataBinder.Eval(Container.DataItem, "EmployeeID")%>" />
    2. ImageButton for Editing: asp:ImageButton runat="server" ID="ibtnEdit"
    ImageUrl="images/button_up_edit.jpg" CommandName="Edit" />
    3. Back button: img src="images/button_up_edit.jpg"
    onClick="history.go(-1)"/>

    --Profile.aspx(ItemTemplate)->Profile.aspx(EditItemTemplate)...
    Profile, part 2: When a user clicks Edit, the new controls are "added" for
    the user to make changes. I added several controls, TextBoxes,
    DropDownLists, Validators all straight into the EditItemTemplate part. These
    controls were not used in ItemTemplate (not sure if that makes a difference),
    only EditItemTemplate

    --Profile.aspx(EditItemTemplate)->Update.aspx...
    Update.aspx: There is no datagrid or datalist present here. All I am doing
    is creating 'asp:Label runat="server" ID="lblFullName"' tags and then using
    code behind to take the incoming form values from Profile.aspx and set the
    labels. Let's say I put in a bad date though, the page will render find
    using the Validators presenting an error. But, if I change those values to
    be valid I then get the Failed to load viewstate error...

    So the flow of these 3 pages should be pretty straight forward. I am still
    not good at understanding how controls *should* be created and the way I
    should be using them. I appreciate your help and hope we can get this
    figured out quickly. Thanks!

    "Alvin Bruney - ASP.NET MVP" wrote:

    > how are you adding your controls to the form? This really has nothing to do
    > with the data you are entering. It's more of an issue with how the controls
    > are created and what sort of preprocessing you do to them. I take it that
    > these controls are probably dynamically created with some sort of low-level
    > data manipulation?
    >
    > --
    > Regards,
    > Alvin Bruney [MVP ASP.NET]
    >
    > [Shameless Author plug]
    > The Microsoft Office Web Components Black Book with .NET
    > Now Available @ www.lulu.com/owc
    > Forth-coming VSTO.NET - Wrox/Wiley 2006
    > -------------------------------------------------------
    >
    >
    >
    > "R Reyes" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > The question I'm asking revolves around what is needed to load a

    > viewstate.
    > >
    > > 1) I am using Regular Expression Validators to validate text fields on a

    > page.
    > > --Let's say I have a form set up. After I type in "bad data" in a date
    > > field and click submit, the validators will show up correctly with the

    > errors
    > > I programmed in red saying bad data was entered.
    > > --Now, when I click on submit again (or even the back button) after
    > > correcting the dates, I get the "Failed to load viewstate" error.
    > >
    > > I've tried setting EnableViewState to both true and false but they don't
    > > make a difference. Tutorials online are not helpful either as I think

    > they
    > > go into too much explanation about what a viewstate is, rather than how to
    > > debug this common problem that I can't seem to get past.
    > >
    > > I've coded a decent sized application for modifying Employee data and I
    > > can't get passed this error. I'm not about to start from scratch
    > > either...Can someone explain to me how I can stop this from happening?
    > >
    > > Here is the stack trace (which I am also not very good at figuring out):
    > > [HttpException (0x80004005): 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.]
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +343
    > > System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState)

    > +142
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    > > System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState)

    > +142
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    > > System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState)

    > +142
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    > > System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState)

    > +142
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    > > System.Web.UI.Page.LoadAllState() +527
    > > System.Web.UI.Page.ProcessRequestMain(Boolean
    > > includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3940
    > >
    > > Please help and post any comments as to what I'm doing wrong. Thanks in
    > > advance.

    >
    >
    >
     
    R Reyes, Oct 19, 2005
    #2
    1. Advertising

  3. R Reyes

    R Reyes Guest

    My last post was a bit longer, so in short I will just say:
    Yes, they are dynamically created when a user clicks on the edit button
    because it runs the EditItemTemplate part of the Profile.aspx page, where
    ItemTemplate doesn't have any controls. And that's where I come to the
    problem of understanding viewstate...

    "Alvin Bruney - ASP.NET MVP" wrote:

    > how are you adding your controls to the form? This really has nothing to do
    > with the data you are entering. It's more of an issue with how the controls
    > are created and what sort of preprocessing you do to them. I take it that
    > these controls are probably dynamically created with some sort of low-level
    > data manipulation?
    >
    > --
    > Regards,
    > Alvin Bruney [MVP ASP.NET]
    >
    > [Shameless Author plug]
    > The Microsoft Office Web Components Black Book with .NET
    > Now Available @ www.lulu.com/owc
    > Forth-coming VSTO.NET - Wrox/Wiley 2006
    > -------------------------------------------------------
    >
    >
    >
    > "R Reyes" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > The question I'm asking revolves around what is needed to load a

    > viewstate.
    > >
    > > 1) I am using Regular Expression Validators to validate text fields on a

    > page.
    > > --Let's say I have a form set up. After I type in "bad data" in a date
    > > field and click submit, the validators will show up correctly with the

    > errors
    > > I programmed in red saying bad data was entered.
    > > --Now, when I click on submit again (or even the back button) after
    > > correcting the dates, I get the "Failed to load viewstate" error.
    > >
    > > I've tried setting EnableViewState to both true and false but they don't
    > > make a difference. Tutorials online are not helpful either as I think

    > they
    > > go into too much explanation about what a viewstate is, rather than how to
    > > debug this common problem that I can't seem to get past.
    > >
    > > I've coded a decent sized application for modifying Employee data and I
    > > can't get passed this error. I'm not about to start from scratch
    > > either...Can someone explain to me how I can stop this from happening?
    > >
    > > Here is the stack trace (which I am also not very good at figuring out):
    > > [HttpException (0x80004005): 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.]
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +343
    > > System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState)

    > +142
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    > > System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState)

    > +142
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    > > System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState)

    > +142
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    > > System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState)

    > +142
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    > > System.Web.UI.Page.LoadAllState() +527
    > > System.Web.UI.Page.ProcessRequestMain(Boolean
    > > includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3940
    > >
    > > Please help and post any comments as to what I'm doing wrong. Thanks in
    > > advance.

    >
    >
    >
     
    R Reyes, Oct 19, 2005
    #3
  4. R Reyes

    R Reyes Guest

    Not sure if you're still looking into this but I came across an article that
    said the controls need to be added first before setting their
    properties/values.

    Does this mean that the code to dynamically add controls must be in the .cs
    file? I have them hardcoded with "asp:Control" tags in the .aspx file
    however for some controls like DataList, the controls are ONLY used in the
    EditItemTemplate part of the code (where they get created because a user
    would have clicked "edit") whereas the ItemTemplate part is only used for
    displaying values from the database, not modifications.

    Maybe I have to add all the controls programmatically with C# only and not
    hardcoded with ASP.NET to prevent that viewstate error?

    "Alvin Bruney - ASP.NET MVP" wrote:

    > how are you adding your controls to the form? This really has nothing to do
    > with the data you are entering. It's more of an issue with how the controls
    > are created and what sort of preprocessing you do to them. I take it that
    > these controls are probably dynamically created with some sort of low-level
    > data manipulation?
    >
    > --
    > Regards,
    > Alvin Bruney [MVP ASP.NET]
    >
    > [Shameless Author plug]
    > The Microsoft Office Web Components Black Book with .NET
    > Now Available @ www.lulu.com/owc
    > Forth-coming VSTO.NET - Wrox/Wiley 2006
    > -------------------------------------------------------
    >
    >
    >
    > "R Reyes" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > The question I'm asking revolves around what is needed to load a

    > viewstate.
    > >
    > > 1) I am using Regular Expression Validators to validate text fields on a

    > page.
    > > --Let's say I have a form set up. After I type in "bad data" in a date
    > > field and click submit, the validators will show up correctly with the

    > errors
    > > I programmed in red saying bad data was entered.
    > > --Now, when I click on submit again (or even the back button) after
    > > correcting the dates, I get the "Failed to load viewstate" error.
    > >
    > > I've tried setting EnableViewState to both true and false but they don't
    > > make a difference. Tutorials online are not helpful either as I think

    > they
    > > go into too much explanation about what a viewstate is, rather than how to
    > > debug this common problem that I can't seem to get past.
    > >
    > > I've coded a decent sized application for modifying Employee data and I
    > > can't get passed this error. I'm not about to start from scratch
    > > either...Can someone explain to me how I can stop this from happening?
    > >
    > > Here is the stack trace (which I am also not very good at figuring out):
    > > [HttpException (0x80004005): 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.]
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +343
    > > System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState)

    > +142
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    > > System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState)

    > +142
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    > > System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState)

    > +142
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    > > System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState)

    > +142
    > > System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +290
    > > System.Web.UI.Page.LoadAllState() +527
    > > System.Web.UI.Page.ProcessRequestMain(Boolean
    > > includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3940
    > >
    > > Please help and post any comments as to what I'm doing wrong. Thanks in
    > > advance.

    >
    >
    >
     
    R Reyes, Oct 20, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Kirksey

    Failed to load ViewState

    John Kirksey, Jul 11, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    2,167
    John Kirksey
    Jul 22, 2003
  2. sck10
    Replies:
    6
    Views:
    917
    sck10
    Sep 1, 2006
  3. Josema
    Replies:
    2
    Views:
    351
  4. Josema
    Replies:
    0
    Views:
    209
    Josema
    May 9, 2005
  5. Albert-Jan Roskam
    Replies:
    2
    Views:
    84
    Chris Angelico
    Nov 25, 2013
Loading...

Share This Page