CompositeDataBoundControl and "Failed to load viewstate"

S

Samuel

hi,

I am new to this CompositeDataBoundControl, and I have an issue with loading
viewstate on Postback. The error message is as follows:

"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."

What my control does is it needs to build both its structure (textbox,
dropdownlist, etc) and their respective data based on values from the db.

It is used to present a set of custom registration fields where user can
design what fields they want to display on the form, and each field can be
represented in either textbox, dropdownlist, radiobuttonlist, or
checkboxlist. The preferences are saved in the database.

So, the control interacts with 2 sets of data, one for its structure and
default values (in the case of listcontrol), and another for the user's
selection when users want to edit the data afterward (not needed for
registration).

What I am doing is I am creating both structure (the control will get the
data by itself without any input) and binding the data (from
control.datasource) in the overloaded CreateChildControls function:

CreateChildControls(dataSource As IEnumerable, dataBinding As Boolean) As
Integer

When the control is in Editing mode (both sets of data available), it works
fine because I have to call databind. If it is in Registration mode (no user
data, but structure data only) or Postback (where databind is not called),
the control is entirely empty (no structure at all) and errored because
without calling databind the CreateChildControls function is not fired.

Am I doing something wrong, or should I be looking to inherit from a
different class than CompositeDataBoundControl?

Thanks in advance!
 
W

Walter Wang [MSFT]

Hi,

Based on your description so far, I think inheriting from
CompositeDataBoundControl is fine here since the control hierarchy will be
based some dynamic data source.

The CreateChildControls method will be called under two distinct scenarios:
when your control is databound (and a dataSource is supplied) and on
postback (when a data source is NOT available). Under this second scenario,
the dataSource value will be an array of null's of the length you returned
when the control was first databound. It is up to you to recreate enough of
the control structure in the second scenario for the event code to work. --
I suspect you're not recreating the child controls in this case, and caused
the ViewState failed to restore correctly.

Dino Esposito's book <<Programming Microsoft ASP.NET 2.0 Applications:
Advanced Topics>> Chapter 14 has detailed description of how to create a
CompositeDataBoundControl. You can download the example code here:
http://www.microsoft.com/mspress/companion/0-7356-2177-2/

Please feel free to post some code if you have anything unclear. Thanks.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Samuel

I actually followed exactly what you suggested - the book by Dino Esposito.

I got my control working by inheriting CompositeControl instead of
CompositeDataBoundControl. CompositeControl is straightforward (I just add
the datasource property and override the databind sub) and since I am hitting
the db anyway on each page load to build the structure, I don't have to worry
about the viewstate thing.

What I am unclear about CompositeDataboundControl is that the
CreateChildControls returns an integer representing number of "items". How
then do we use this number when dataBinding is false?

In my case, without calling databind() on the control, the
CreateChildControls didn't fire at all. It must at least fire to build the
control structure from Viewstate, right?

And since the structural controls are dynamically added, do I not have to
add them back on each page load (in my case, query the db to get what type of
controls to add)? As I know it, Viewstate only persists the data (text for
textbox, instead of the whole textbox control), and if I don't load the
structural control (textbox control e.g.) myself, the viewstate cannot be
saved.
 
W

Walter Wang [MSFT]

Hi,

Using Dino's sample
http://localhost/ProAspNetAdv/Samples/Ch14/BarChart/TestBarChart.aspx for
example, if you add a breakpoint in BarChart.cs: CreateControlHierarchy:
after the barchart gets loaded, click "Just post back": you will notice the
"dataBinding" is false but "dataSource" contains 9 null item in an array.
This 9 is returned in previous call to this CreateControlHierarchy when
"dataBinding" is true. In this case, although the data is null, but the
BarCharItems still get created correctly.

As far as I know, DataBind() must be called for the
CompositeDataboundControl since it's not saving state into ViewState.

I understand Dino's BarChart example is not similar to your case since your
control has EditMode. Do you think it's very much like the FormView or
DetailsView (which are also inherited from CompositeDataboundControl). You
might want to take a look at their implementation using Reflector
(http://www.aisto.com/roeder/dotnet/).

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi,

Please feel free to reply here if you have anything unclear. Thanks.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top