Professional Web Applications Themes

CompositeDataBoundControl and "Failed to load viewstate" - ASP.NET Building Controls

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

  1. #1

    Default CompositeDataBoundControl and "Failed to load viewstate"

    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!
    Samuel Guest

  2. #2

    Default RE: CompositeDataBoundControl and "Failed to load viewstate"

    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 (microsoft.com, 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 ysis and dump ysis 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.

    Walter Guest

  3. #3

    Default RE: CompositeDataBoundControl and "Failed to load viewstate"

    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.
    Samuel Guest

  4. #4

    Default RE: CompositeDataBoundControl and "Failed to load viewstate"

    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 (microsoft.com, 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.

    Walter Guest

  5. #5

    Default RE: CompositeDataBoundControl and "Failed to load viewstate"

    Hi,

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

    Regards,
    Walter Wang (microsoft.com, 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.

    Walter Guest

Similar Threads

  1. Failed to load viewstate. The control tree into which viewstate...
    By Josema in forum ASP.NET Building Controls
    Replies: 5
    Last Post: April 13th, 07:48 AM
  2. Failed to load viewstate
    By John Kirksey in forum ASP.NET Building Controls
    Replies: 14
    Last Post: October 11th, 10:14 PM
  3. CreateChildControls and Exception Failed to Load ViewState
    By Craig in forum ASP.NET Building Controls
    Replies: 2
    Last Post: February 16th, 07:49 PM
  4. Failed to load viewstate on editable datagrid
    By steve gibson in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: January 28th, 01:28 PM
  5. Replies: 0
    Last Post: July 25th, 04:58 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139