Help with Loading ASCX control during PostBack

Discussion in 'ASP .Net' started by John Cosmas, Aug 28, 2004.

  1. John Cosmas

    John Cosmas Guest

    I've got a page which loads up a different user control into a placeholder
    control every time a button is clicked on the parent page. I use a
    statement like Me.plcTabViews.Controls.Add(LoadControl("test.ascx")). There
    are up to 30 possibilities which are decided by CASE statements and keeps
    one from loading up onto another. However, I'm running into a unique
    error...

    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.

    This leads me to believe that in my code, I've not done something correctly.
    It works fine loading up controls when I click or load several of the pages,
    but it may fail after a few parent button/option clicks. Please advice!

    TIA
    John Cosmas
    John Cosmas, Aug 28, 2004
    #1
    1. Advertising

  2. John Cosmas

    Karl Guest

    John,
    Your code is fine. There are a couple ways to deal with the problem. But
    first a brief explanation. Basically when you add a control to the page,
    the controls within it (grids, dropdownlists...) will store their values in
    the viewstate. On postback, it goes through the viewstate and reassigns the
    values stored there into the controls. If your controls don't match up, you
    get the error you got. Example:

    1st click --> Load test.ascx -->
    Me.plcTabViews.Controls.Add(LoadControl("test.ascx")) --> test.ascx loads
    whatever it needs to into the viewstate (as the first child of plcTabViews)
    2nd click --> Load test2.ascx -->
    Me.plcTabViews.Controls.Add(LoadControl("test2.ascx")) --> grabs the
    viewstate for the first child on plcTabViews and tries to load it into
    test2.ascx --> CRASH

    Your solutions are:
    - Disable viewstate on all your controls. You can do this by putting
    EnableViewState="False" in the @Control directive of each control. If those
    controls need viewstate, they won't behave as expected.

    - Reload the previous control and then clear it, then load the control you
    want. Using the above example, the 2nd click would look like this:
    2nd click --> Load test.ascx --> it grabs the viewstate --> Remove
    test.ascx --> Load test2.ascx --> ...
    This will give you extra overhead but can fairly easily be done

    - Instead of using postback to load controls, don't postback and use
    querystring values.

    Considering you aren't reloading the controls, my guess is you don't need
    anything from their viewstate and the 1st option will be your best bet.

    Karl


    "John Cosmas" <> wrote in message
    news:uX$...
    > I've got a page which loads up a different user control into a placeholder
    > control every time a button is clicked on the parent page. I use a
    > statement like Me.plcTabViews.Controls.Add(LoadControl("test.ascx")).

    There
    > are up to 30 possibilities which are decided by CASE statements and keeps
    > one from loading up onto another. However, I'm running into a unique
    > error...
    >
    > 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.
    >
    > This leads me to believe that in my code, I've not done something

    correctly.
    > It works fine loading up controls when I click or load several of the

    pages,
    > but it may fail after a few parent button/option clicks. Please advice!
    >
    > TIA
    > John Cosmas
    >
    >
    Karl, Aug 28, 2004
    #2
    1. Advertising

  3. John Cosmas

    Quintin79

    Joined:
    Mar 26, 2007
    Messages:
    1
    awesome.

    this is exactly my problem and your ideas have opened my eyes to the solution. thanks so much.
    Quintin79, Mar 26, 2007
    #3
  4. John Cosmas

    anandkavi

    Joined:
    Oct 20, 2010
    Messages:
    1
    How to Remove user controls?

    protected void Page_Load(object sender, EventArgs e)
    {
    // Put user code to initialize the page here
    //Default Selection is View Forecast

    if (ViewState["ViewForecastFirstTime"] == null)
    {


    Controls.Add(LoadControl("ViewForecast.ascx"));


    }

    if (Request.Form["__EVENTTARGET"] == "btnUploadForecast")
    {

    Controls.Add(LoadControl("UploadForecast.ascx"));


    }

    if (Request.Form["__EVENTTARGET"] == "btnViewForecast")
    {

    Controls.Add(LoadControl("ViewForecast.ascx"));


    }

    if (Request.Form["__EVENTTARGET"] == "btnNewForecast")
    {

    Controls.Add(LoadControl("NewForecast.ascx"));


    }

    if (Request.Form["__EVENTTARGET"] == "btnPortView")
    {

    this.Controls.Add(LoadControl("PartView.ascx"));



    }
    }

    Is this possible? How to remove the User control?
    anandkavi, Oct 20, 2010
    #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. Quentin
    Replies:
    1
    Views:
    473
    Quentin
    Jul 29, 2003
  2. Jason Shohet
    Replies:
    1
    Views:
    472
    Kevin Spencer
    Nov 10, 2003
  3. Holger (David) Wagner
    Replies:
    2
    Views:
    1,929
    Holger (David) Wagner
    Jul 3, 2004
  4. Joe
    Replies:
    1
    Views:
    361
    Jeffrey Tan[MSFT]
    Feb 16, 2004
  5. Replies:
    2
    Views:
    524
Loading...

Share This Page