Problems with IPostBackDataHandler interface?

Last post 06-16-2009 3:22 PM by rmdw. 1 replies.

Sort Posts:

  • Problems with IPostBackDataHandler interface?

    06-16-2009, 2:21 PM
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 7:03 PM
    • Vancouver, BC, Canada
    • Posts 876

    I'm following up on a previous post but targeting in where I now think the problem lies.  Here's my situation:

    I have an ASP.Net 3.5 web project written in C#.  I am trying to introduce a toolbar which is implemented as follows:

    • Custom built toolbar, which consists of link buttons.  It resides in a User Control.
    •   The User Control is instantiated at the top of a Master Page.
    •     The Master Page is referenced by several Content Pages.

    I've constructed things in such a way that I can customize the names & images of each toolbar button.  So, for example, one Content Page may display these toolbar buttons:  Add Edit Move Save.  While another Content Page may display these toolbar buttons:  Insert Delete Save.  Any combination of the known button types is permissible.

    As you're probably envisioning, the toolbar buttons are instantiated dynamically, depending on what parameters are passed to the 'CreateCommandButtons' method within the User Control.

    Everything is working fine except for one crucial thing: When I change the enabled/disabled setting of each toolbar button, the value is not remembered after a postback; in my particular case, it's a partial postback on an AJAX enabled page.

    So I went back to basics and decided to simulate the same approach in a small test project, which you can download here: http://mwtech.com/downloads/public/Dynamic_Button_Creator.zip

    Here's what you need to do:

    1. Run the WebForm1.aspx page in this project.
    2. Press the "Press Me to Alter Toolbar Buttons" button a few times. It causes a postback and increments the numeric value of each of the 3 toolbar link buttons.
    3. Now press "Enable Center Button".  Notice how the center toolbar button turns blue, to denote it's enabled.
    4. Now press the same button as in Step #2 again.  Postbacks continue to occur and yet the center button stays blue (ie. enabled).

    So to my great surprise, the test project works fine!!  Yet very similar code within my actual project is not working.  Instead, there, when you execute Step #4 above, the center button would revert back to gray.

    So I did more reading and it seems that it's the IPostBackDataHandler interface that is responsible for restoring the altered control values.  Yet it's not doing this in my real project  I'm hoping that someone familiar with IPostBackDataHandler can tell me what key things to look for to see why it's not functioning properly.

    Robert

    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
  • Re: Problems with IPostBackDataHandler interface?

    06-16-2009, 3:22 PM
    Answer
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 7:03 PM
    • Vancouver, BC, Canada
    • Posts 876

    After about 5 hours of scratching my head on this, I finally found a solution ... though I don't know why it works!

    I've long followed the practice of setting: EnableViewState="false" on any page which I didn't feel needed ViewState.  This seemed to be a good practice.

    And in the case of IPostBackDataHandler, articles like this one seemed to confirm that it was fine to turn off the View State: http://www.codeproject.com/KB/viewstate/ViewState.aspx

    Yet when I noticed that my test Content Page had EnableViewState="false"  I set it to true and ... voila, suddenly my User Control toolbar had its changed values restored once again!

    Then I set EnableViewState back to false in the Content Page and explicitly set it to true in the User Control.  It did not work.

    So there's a bit of a mystery here.  Certainly I have things working now but I would love to hear from a State Management guru as to why things are occurring as they are!

    Robert

    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
Page 1 of 1 (2 items)