Failed to load view state error when moving dynamically created widgets around the raddocklayout

30 posts, 0 answers
  1. Martin Beeby
    Martin Beeby avatar
    60 posts
    Member since:
    Nov 2006

    Posted 21 Sep 2007 Link to this post

    This post contains a solution for the ViewState error that occurs in the excellent PortalSite demo, which can be found here: http://www.telerik.com/community/forums/thread/b311D-mkttd.aspx

    This problem had me pulling my hair out. As I started to add complex user controls to the raddocklayout I would often receive a ViewState error as I dragged the various controls around.

    At first the problem appeared to be random but on closer inspection there was a pattern.

    The property PageWidgets selects and sorts the docks so that you can add them to the dock control in the correct order, the pagewidgets property is called as the page is initialized.
     
    Once the page has initialized the viewstate is loaded and the dynamic controls are populated with the ViewState provided during postback. ASP.Net 2.0 uses indexes instead of IDs to figure out which viewstate belongs to which control.

    Because the sort method sorts the widgets by Index (which changes depending on the order of the widgets), the widgets are being added in a different order every time the page is initialized, however the ViewState is being applied in the same order. So basically the ViewState is being applied to the wrong control. Some times this causes problems, sometimes it dosen’t depending on the user control in the widget.

    Luckily the soultion is simple. In asp.net 2.0 you can force viewstate to be applied to a control based upon the ID of the control and not the index on the control.

    To do this you need to create a new class that is going to act like a wrapper for the rad controls so that you can attach the ViewStateModeById attribute:

    Create a new .vb file in your app_code folder and paste in the following code

     

    Imports Telerik.Web.UI  
    Namespace Martinbeeby.Core.Controls  
     
    <ViewStateModeById()> _  
        Public Class RadDockNew  
            Inherits RadDock  
        End Class 
        <ViewStateModeById()> _  
        Public Class RadDockLayout New 
            Inherits RadDockLayout  
        End Class 
     
        <ViewStateModeById()> _  
       Public Class RadDockZoneNew  
            Inherits RadDockZone  
        End Class 
    End Namespace 
     

    In your web config file under System.web > Pages > controls> add the following:

    <add tagPrefix="Martinbeeby" namespace="Martinbeeby.Core.Controls"/> 

    You're config will look something like this:

    <?xml version="1.0"?>  
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">  
    <system.web> 
       
    <pages enableEventValidation="true">  
     <controls> 
      <add tagPrefix="Martinbeeby" namespace="Martinbeeby.Core.Controls"/>  
     </controls> 
    </pages> 
     
    </system.web> 
    </configuration> 
     
     

    Then on you page  change the control names from :

    telerik:RadDock to Martinbeeby:RadDockNew
    telerik:RadDockLayout to Martinbeeby:RadDockLayoutNew
    telerik:RadDockZone to Martinbeeby: RadDockZoneNew

     

    <Martinbeeby:RadDockNew id="DockLayout" runat="server" storelayoutinviewstate="false" onsavedocklayout="DockLayout_SaveDockLayout">  
        <div> 
        <Martinbeeby:RaddockzoneNew runat="server" id="ZoneLeft" style="float: left; margin-right: 10px;" 
          width="45%" minheight="200px">  
         </ Martinbeeby:RaddockzoneNew > 
         < Martinbeeby:RaddockzoneNew runat="server" id="ZoneRight" style="float: left;" minheight="200px" 
          width="45%">  
         </ Martinbeeby:RaddockzoneNew > 
        </div> 
        <div style="display: none">  
         <asp:updatepanel runat="server" id="UpdatePanel1">  
          <triggers> 
           <asp:asyncpostbacktrigger controlid="ButtonAddDock" eventname="Click" /> 
          </triggers> 
         </asp:updatepanel> 
        </div> 
       </ Martinbeeby:RaddockzoneNew > 
     

    This should fix the problem.

    Hope this helps someone.

  2. Guy
    Guy avatar
    49 posts
    Member since:
    Oct 2007

    Posted 29 Oct 2007 Link to this post

    Martin,

    This appears to be the fix i am looking for, however i get an unknown tag error when i load the page.

    i followed the instructions exactly as you wrote them but for some reason a parser error occurs telling me that the tag prefix is not registered.

    Have telerik actually fixed this themselves yet?
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Martin Beeby
    Martin Beeby avatar
    60 posts
    Member since:
    Nov 2006

    Posted 29 Oct 2007 Link to this post

    It's not so much a problem with the control, it's really a problem with the demo.

    Did you modify your web config file and add <add tagPrefix="Martinbeeby" namespace="Martinbeeby.Core.Controls"/>  ?

    This is what registers the control with your application so that you can use it without compiling the code.

  5. Guy
    Guy avatar
    49 posts
    Member since:
    Oct 2007

    Posted 29 Oct 2007 Link to this post

    Hi Martin,

    I certainly did. i get the error

    Parser Error Message:
    Unknown server tag 'Martinbeeby:RadDockLayoutNew'.

    In objectBrowser the fully qualified namespace of the classes comes up as Infonetica.Red.Martinbeeby.Core.Controls however i have tried the web.config with just your namespace and with the one stated above.

    After changing the tag prefixes in the aspx page and switching to design view everything looks ok. its just when i run it.

    What is the root namespace of the project that you created the class in? this could be where im getting it wrong, as yours may have overlap by default.

    I fully understand the source of the issue that you wrote up, but was really struggling to find a workaround to it until i saw your post.  thanks for your help so far.

    Ive spent the last 4 days trying to think of a better way to engineer the whole layout thing to workaround this issue and a couple more such as different zone layouts per user etc..
  6. Martin Beeby
    Martin Beeby avatar
    60 posts
    Member since:
    Nov 2006

    Posted 29 Oct 2007 Link to this post

    Yes sounds like that's the problem.

    In the example I added here the default namespace is MartinBeeby.

    Have you nested this namespace inside your own namespace?

    prehaps you could send me the project zipped up and i could take a look? Or is the file too big?
  7. Guy
    Guy avatar
    49 posts
    Member since:
    Oct 2007

    Posted 29 Oct 2007 Link to this post

    Hi Martin,

    Im pleased to say i got it working - the page required a register directive for the tagPrefix because it is not marked on the assembly (which can be done in assemblyInfo if necessary).

    i got the clue from the following link, although im using web app projects so had to modify the assembly reference to my own dll as app_code directory does not exist.... http://alpascual.com/blog/al/archive/2007/04/30/Fixing-the-error-Unknown-server-tag-when-you-create-or-inherit-a-control-on-App_5F00_code-folder.aspx

    Many thanks for your help, you have no idea how grateful i am! :-D

    Regards,
    Guy
  8. Martin Beeby
    Martin Beeby avatar
    60 posts
    Member since:
    Nov 2006

    Posted 29 Oct 2007 Link to this post

    One last thing, There is a slight bug with the code below.

    I haven't had a chance to fix it properly, but i do have a work around. The add button on the portal can still create view state problems.

    One of our testers discovered a bug that when you add more than one new widget to you page in one session. The veiwstate gets applied from the first control to the second control.

    This has something to do with the page control hierarchy, as the control is added to the updatepanel and not to the raddock.

    My work around is to simply remove the Ajax link on the add button. (It sucks I know) So that the button causes postback.

    To be honest this works fine for my needs and I didn't have time to investigate the problem completely.

  9. Guy
    Guy avatar
    49 posts
    Member since:
    Oct 2007

    Posted 30 Oct 2007 Link to this post

    ok, thanks for the heads up.

    i havent been able to reproduce the bug you speak of but will keep an eye out for it.

    incase anyone else needs this solution, the best way to make it work is the following....

    follow martins instructions for all the code, but set the namespace in the class to something of your choice - i used 'Fixes'.

    because the root namespace for my web project is already GHC this means that these classes live in the namespace GHC.Fixes

    so in your web.config where you add the tagprefix put the full namespace for the class also... Note that my tags will be prefixed with 'Fixes' rather than 'martinbeeby'
    <system.web>
    <add tagPrefix="Fixes" namespace="GHC.Fixes"/>

    Then in each page that you use these new controls you must add a register directive at the top of the page like so...

    <%@ Register TagPrefix="Fixes" NameSpace="GHC.Fixes" Assembly="GHC" %>

    The Assembly attribute is the name of the dll (minus the extension) that contains the controls.  My dll compiles down to GHC.dll so my assembly attribute has a value of GHC.

    ns2.discountasp.net

  10. Martin Beeby
    Martin Beeby avatar
    60 posts
    Member since:
    Nov 2006

    Posted 30 Oct 2007 Link to this post

    Ok what the mistake may have been

    You don't need the <%@ Register TagPrefix="Fixes" NameSpace="GHC.Fixes" Assembly="GHC" %> on each page

    Thats what the <add tagPrefix="Fixes" namespace="GHC.Fixes"/> is for.

    How ever you probably need to register the namespace in the web config as below:

    <pages enableEventValidation="false">  
    <namespaces> 
                <add namespace="Martinbeeby.Core"/>  
                </namespaces> 
     <controls> 
     <add tagPrefix="Martinbeeby" namespace="Martinbeeby.Core.Controls"/>   
        <!-- added line below for ajax support --> 
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>  
       </controls> 
      </pages> 


     

  11. Bnva1
    Bnva1 avatar
    1 posts
    Member since:
    Oct 2007

    Posted 13 Mar 2008 Link to this post

    Martin,

    You are the man - been banging my head against the wall with this VS error for a few days.  Your fix worked like a charm.  This thread is pretty old, I don't understand why Telerik hasn't incorporated your fix into the latest 2 releases.

    Thanks again,
    Brian
  12. Martin Beeby
    Martin Beeby avatar
    60 posts
    Member since:
    Nov 2006

    Posted 13 Mar 2008 Link to this post

    I'm really glad this post helped. I'm not sure why the problem hasn't been addessed in the latest versions of the controls.

    I guess It's not really a problem with the dock control. Its more a limitation of the portal demo.

    Who cares I guess. As with most things, once you understand the problem it's pretty easy to fix.

  13. Jerald Carter
    Jerald Carter avatar
    16 posts
    Member since:
    Dec 2005

    Posted 30 Dec 2008 Link to this post

    Beautiful!

    Worked like a charm!
  14. Joseph
    Joseph avatar
    32 posts
    Member since:
    Oct 2008

    Posted 30 Dec 2008 Link to this post

    The fix worked for me as well.  ViewStateModeById is a wonderful thing.  This is a .NET thing of course and not really a Telerik issue.  Anytime I add dynamic controls to a page now, my standard practice is to subclass the control and use ViewStateModeById.  Thanks again Martin.

    joseph
  15. Stuart
    Stuart avatar
    6 posts
    Member since:
    Feb 2009

    Posted 02 Feb 2009 Link to this post

    Just wanted to say a big thank you for posting this fix. I too have spent time trying to work out the issue and your fix worked like a dream.

    Cheers,
    Stuart,

  16. Sameer
    Sameer avatar
    12 posts
    Member since:
    Oct 2008

    Posted 12 Feb 2009 Link to this post

    Another success Martin.You saved me..

    It should be marked as very IMP fix.
    People would save lot of time if coded like that to begin with (but then they will not realize how tough it was otherwise :) )


  17. Martin Beeby
    Martin Beeby avatar
    60 posts
    Member since:
    Nov 2006

    Posted 12 Feb 2009 Link to this post

    Thanks for the feedback. The fact that people are still using this and also being so kind with the replies makes me glad I made this post over 1 and 1/2 years ago!

    Cheers Martin
    http://www.thewayithink.co.uk
  18. Cradz
    Cradz avatar
    65 posts
    Member since:
    Nov 2005

    Posted 05 Feb 2010 Link to this post

    Just wanted to say thank you.

    This fixed the issue I was having too.
  19. Andii
    Andii avatar
    17 posts
    Member since:
    Nov 2006

    Posted 20 May 2010 Link to this post

    I just wanted to say thank you too. It solved the problem we had for a time now.

    //andii
  20. HL
    HL avatar
    55 posts
    Member since:
    Dec 2009

    Posted 16 Aug 2010 Link to this post

    I just want to say -- yuo saved me lots of time and I appreciate you post the answer. I am so disappointed that Telerik didn't fix it till now and it cost me several days for this issue. THANK-YOU !!!!

    Thanks
    Helena
  21. Dongwook Kim
    Dongwook Kim avatar
    2 posts
    Member since:
    Nov 2009

    Posted 15 Sep 2010 Link to this post

    Hi,

    I really want to try the code in c# but I'm having an error, tye type or namespace name 'ViewStateModeByIdAttribute could not be found.

    Any solutions?

    thanks,
    Ryan
  22. Andii
    Andii avatar
    17 posts
    Member since:
    Nov 2006

    Posted 16 Sep 2010 Link to this post

    You need to add a reference to System.Web
    and then you need add this to the class

     

     

     

     

    using System.Web.UI.WebControls;

     

     

     

     

     

     

     

     

     

     

     

     

  23. Dongwook Kim
    Dongwook Kim avatar
    2 posts
    Member since:
    Nov 2009

    Posted 16 Sep 2010 Link to this post

    Hi,

    Thanks you for your solutions. I haven't tried it yet but I have a question.
    After removing a dock or docks, I got the "failed to load viewstate" error when it posted back to server specially the the dock or docks contains the control using viewstate such as radgrid(paging) or controls(textbox) with button.

    does Anybody know it will solve the my issues?

    Thanks,
    DW
  24. Andii
    Andii avatar
    17 posts
    Member since:
    Nov 2006

    Posted 17 Sep 2010 Link to this post

    Yes, that should take care of your problem.
  25. afishisafish
    afishisafish avatar
    1 posts
    Member since:
    Mar 2010

    Posted 20 Jan 2011 Link to this post

    Thank you for sharing, very helpful solution... cheerz!
  26. Christopher Warin
    Christopher Warin avatar
    1 posts
    Member since:
    Feb 2010

    Posted 20 Jan 2011 Link to this post

    This fixed my issue also. My thinking is that on the first load you add the raddocks to the radlayout control But after a certain number of postbacks or raddock moves the controls are in the raddockzones rather than in the radlayout as set in oninit?  Not sure but I wish this was part of a service pack. Telerik definetly needs to look into this fix and explain why are the implications of using ViewStateModeById
  27. Pero
    Admin
    Pero avatar
    1156 posts

    Posted 21 Jan 2011 Link to this post

    Hello,

    The main reason why the RadDock class is not marked with the ViewStateModeById attribute is because of the performance cost when loading the ViewState information.
    The ViewState problems with the RadDock control come from the fact that after closing a given dock it is not recreated on the next postback, and its ViewState information is loaded to another control placed on its spot in the Controls tree of the Page.

    For the time being the suggested solution for resolving ViewState problems in applications that are highly dependent on the ViewState, is inheriting the RadDock class and marking it with the ViewStateModeById attribute.

    Kind regards,
    Pero
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  28. Michael
    Michael avatar
    4 posts
    Member since:
    Jun 2011

    Posted 25 May 2012 Link to this post

    I was having the same issue ... but when I applied this work around I am getting this error message on runtime

    "The base class includes the field 'RadDockZoneM', but its type (Martinbeeby.Core.Controls.RadDockZoneNew) is not compatible with the type of control (Martinbeeby.Core.Controls.RadDockZoneNew).
    "

    I have cleaned my solution , rebuilt it, removed Telerik.Web.UI refernces and re-added it. I re-organized the folders but still I can'f figure out why this is happening

    In my scenario I only need to decalre a RadDockLayout and a RadDockZone .. the actual RadDocks will be added dynamically in the zone.

    Some help will be much appriciated.

    Thanks in advance
  29. Pero
    Admin
    Pero avatar
    1156 posts

    Posted 28 May 2012 Link to this post

    Hi Michael,

    Is it possible that you might have a circular reference in your project.  Could you please follow the instructions in the links below and see the issues will be resolved?

    http://connect.microsoft.com/VisualStudio/feedback/details/104839/the-base-class-includes-the-field-but-its-type-is-not-compatible-with-the-type-of-control

    http://support.microsoft.com/kb/919284

    All the best,
    Pero
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  30. Michael
    Michael avatar
    4 posts
    Member since:
    Jun 2011

    Posted 30 May 2012 Link to this post

    Hi,

    I managed to fix the problem by adding the MartinBeeby Class in a seperate class library and it worked.

    But it seems it is only partially working. In my zones I dynamically add UserControls. Although I am keeping the items in the RadComboBox for example..  but I am loosing the selected/checked items after postbacks and even the text inputted...

    Thanks againg
  31. Slav
    Admin
    Slav avatar
    1356 posts

    Posted 31 May 2012 Link to this post

    Hi Michael,

    Please check the online demo My Portal as it implements loading of user controls in dynamically added RadDock controls and can be used as a base for achieving the same on your end.

    Note the I am willing to help you, but currently I am mostly guessing as to what your setup is. If you are still having difficulties, please open a support ticket and send a simple, runnable page that isolates the problem so that I can inspect it locally and suggest a concrete solution.

    Regards,
    Slav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017