I have an aspx page that dynamically loads user controls: there is an UpdatePanel that holds these controls (one each time). When I change a control with another one it fails with the message:
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 application. For example,
when adding controls dynamically, the
controls added during a return must
match the type and position of the
controls added during the initial
application.
All of three controls inherit from System.Web.UI.UserControl. Maybe I can solve the problem doing:
Load three controls at start time and then hide two of them as we need it.
Use three UpdatePanels, one for each user control.
Any advice?
Make sure that postbacks aren't updating undesired panels by setting the postback mode to conditional and updating them manually.
When you dynamically add the user controls, are you assigning the id property? And are you adding the control again, on postback, setting the id property to the same value?
Related
I'm trying to create multiple controls by using retrieved data from query, but preventing them from dissapearing on postback, allowing me to get and mantain their values, the problem I have is that I cannot create them on Init because the number of controls, their IDs, and other properties are only known after user selects an item on menu.
Page loads a Menu with all its items and values (Data dependent), plus, a button is loaded too
User clicks a item on menu.
Selected value is used to perform a query by using a Dataset (This happens inside a function which is called from Menu_ItemClick event).
Retrieved data from query is used to determine how many controls must be created (2, 4, 6, etc.). Each control has its unique ID, which is given according to data.
Controls are created and placed into a Panel (named p).
Now controls are visible and available for editing (RadioButtons, TextAreas)
User clicks on button to save information from dynamic controls into a Database
Problems I'm facing
Controls dissapear on postback when clicking button, because they weren't created on Init.
Placing button on UpdatePanel to prevent whole page postback, makes dynamic controls not accesible when trying this:
For Each c In p.Controls
...
Next
The only control it gets is a single Literal control (Controls count is 1), as if other controls didn't exist.
Thank you in advance.
When you wrote "Controls dissapear on postback when clicking button, because they weren't created on Init", did you mean to say that "Controls dissapear on postback when clicking button, because they weren't re-created on Init"? If not, then that is likely a root cause of your problem - dynamically-created controls must always be recreated in response to a PostBack (cf. ASP.NET dynamically created controls and Postback). There may be other issues as well, as dynamic controls in Web Forms can provide a lot of challenges as your scenario gets more involved - here's one article that lays out many of them under various scenarios http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx (e.g., if the user can re-select from the DropDownList to generate a different set of dynamic controls). The canonical reference on all of this is http://msdn.microsoft.com/en-us/library/ms178472.aspx.
Now, on PostBack you'll need some way to ascertain which controls were dynamically created so they can be dynamically re-created. As such, you'll need to store somewhere whatever information allowed you to dynamically create the controls. Since ViewState isn't available in Page_Init and there can be other issues introduced when using sessions, my suggestion is to simply declare a HiddenField that contains that state information. In Page_Init, you'll then need to get the HiddenField's value from Request.Form (since the value of your HiddenField won't be loaded until after Page_Init from ViewState) and go from there to re-create your controls.
My final suggestion: try getting everything working with a regular Panel first and then try and introduce the UpdatePanel - no need to over-complicate the problem at first.
I want to assign values to server control properties in my code behind to "initialize" a form. I don't need/want these values to be added to the viewstate. (I know I can disable viewstate for a specific control, but I need viewstate as some values may change and need to be persisted during postbacks.)
At the moment for me it seems its not possible to load these values in code without having them added to the viewstate.
As I understand the following happens:
Page: PreInit
I could add values to SOME controls (its working for example with a literal control to set the text value) here BUT since control's init methods are executed later I cannot for example find a RegisterUser control and its child controls at this stage yet, they are still null. > so no solution in this specific case, there's more cases
Control: Init
I cannot reach this point from within my page, this can only be used inside the user control code for example when you write your own usercontrol and want to initialize its values, I ofcourse dont want to create my own control for each control I need to initialize with executing some logic first.
Control: TrackViewState
Viewstate Tracking is enabled so from here on after anything I set will be marked as 'dirty' and therefore added to the viewstate.
Page:Init
Page:TrackViewState
So am I right in saying:
In code behind, without disabling a controls viewstate property..
You can initialize "1st level" child control properties from the page control in the PreIinit method without having the value being added to the viewstate.
You cannot initialize Child control properties from controls that are "1st level" child controls of the page without having the value added to the viewstate.
?
Then,
You can initialize control properties declaratively by using resources, but this does not circumvent the scenario where you need to execute logic
You can execute logic by using databinding syntax but you would have to manually call databind on the control having the same issues as above
Is there no way to iterate a set of controls/child controls and initiate values in code behind withouth having these values added to the viewstate?
With no reactions here and some further research, I am now at the point to conlude that you can indeed NOT iterate a set of controls/childcontrols and initiate values in code behind without having these values added to the viewstate, because of reasons mentioned in the question.
There are some partial solutions however as made clear here: http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx
One possibility is to hook into the init event of the control itself declaratively, you would have to do that for each control. In code behind, inside the event handler you can set the property and since viewstate is not tracking yet it will not be saved in viewstate.
There are more options for different scenario's for example initializing dynamically added child controls inside a custom control by overriding the CreateChildControls method. Definately worth reading the above mentioned 'Truly Understanding Viewstate' link.
I am trying to design a page that handles both Employee and Station CRUD tasks depending on the user's preference. I have sub-classed UserControl to create an EditEntityControl and developed two custom controls that derive from this base class and each handle the CRUD activities for either the Employee or Station object.
When the user toggles the value in a dropdownlist, which triggers a postback, I want to dynamically load the correct control into the page. This works on the first load of each control, but when I try to reload the first control (after loading the second), I get the following 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.
I also see some strange behavior on the initial load of the second control where data bindings don't bind to the correct controls (setting the text of a button rather than a textbox on the control for example).
Is there a way to handle this scenario? Or, is there a way to clear out the ViewState and just re-request the page entirely to get around this error? It appears that if I could clear up this ViewState clutter/confusion between PostBacks, everything else is working as designed.
Great suggestions in the comments above, but what finally pointed me to the correct solution was Joel Coehoorn's answer on this question.
I load up the control OnInit, then in the SelectionChanged event blow away the OnInit changes and recreate the correct control as needed. Thanks for the additional suggestions about making unused controls invisible. I will keep that in mind for future reference.
The overall problem is that I have a web application that has many hidden controls that are shown and hidden based on user rights or based on particular actions within the main page. Because there are so many hidden controls and the combination of each of these control's databinds paired with the sheer size of the resulting rendered HTML, the page is too slow to load. We can of course move the databinds to only occur on the show of the controls but this only addresses half of the problem. We are attempting to address both by Dynamically loading the usercontrols. As we all know the problem with this approach is that when dynamically loading the control, it must be re-added to the page on every subsequent request. So anytime say a dropdown in selected on the dynamic child control that causes an action (like OnClick event), it has to reload the child control, which by itself isn't bad but you also have to rebind all the controls on the page or your databound controls will all be empty. So is there anyway to get around having to rebind all of the controls with every load of the child control? I know that for simple things like a textbox I could save the viewstate and the text would persist between requests and reloading the control, but say the child control has a grid with several hundred rows of data after it is bound, how do I persist that when reloading the child control? I can post a small example if needed.
use Update Panel, i think i will call to server only for updated data not whole page or control.
I have an aspx page which is loading some controls dynamically on postback (ie when a click event happens) and adding them to a placeholder. Depending on what is clicked a different set of controls needs to be loaded.
This works perfectly the first time, but when I clear the placeholder's controls and attempt to dynamically load a different set of controls I get the following 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."
This happens even if I do ViewState.Clear().
Is there a way to do this?
Yuriy Solodkyy explains it well here: Dynamically Created Controls in ASP.NET
When dynamically creating controls you must ensure that every control you create has an unique id.
I think what is happening here is that you are naming your controls like: Control1, Control2, Control3.
And perhaps when you click to create a different set of controls you might give the same name to a different type of object, lets say Control1 was first created like a Textbox and when you click its a checkbox.
Without seeing the code is tough to tell why ViewState.Clear() is not working, but if you are trying to clear the viewstate you don't need a postback, you can try requesting a new page and pass the arguments on the querystring instead of a postback. This will be faster too as you don't have to send the ViewState information back to the server.