Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Help with Loading ASCX control during PostBack

Reply
Thread Tools

Help with Loading ASCX control during PostBack

 
 
John Cosmas
Guest
Posts: n/a
 
      08-28-2004
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


 
Reply With Quote
 
 
 
 
Karl
Guest
Posts: n/a
 
      08-28-2004
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.asc x")) --> 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" <(E-Mail Removed)> wrote in message
news:uX$(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Quintin79 Quintin79 is offline
Junior Member
Join Date: Mar 2007
Posts: 1
 
      03-26-2007
awesome.

this is exactly my problem and your ideas have opened my eyes to the solution. thanks so much.
 
Reply With Quote
 
anandkavi anandkavi is offline
Junior Member
Join Date: Oct 2010
Posts: 1
 
      10-20-2010
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TabStrip/Multipage using UserControl (.ascx) contained within a UserControl(.ascx) shaileenpatel@yahoo.com ASP .Net Web Controls 2 05-03-2005 05:01 PM
Multiple *.ascx files with a single *.ascx.cs in VS.NET 2003 Holger (David) Wagner ASP .Net 2 07-03-2004 09:23 AM
datasource of child .ascx not visible to parent .ascx Joe ASP .Net Web Controls 1 02-16-2004 07:27 AM
can a dg be added to an ascx? ascx call a ws? Jason Shohet ASP .Net 1 11-10-2003 07:08 PM
[ASCX] Add an ascx in a webcontrol... Quentin ASP .Net 1 07-29-2003 07:37 PM



Advertisments