Question

I feel like I may be overlooking a fundamental concept of the page life-cycle here and have been (either because I can't figure out the right keywords or it hasn't been asked) unable to locate an existing answer so forgive me if this has been asked.

Basically, I need to persist a mutable object between the client side and the server side. Since the viewstate is encrypted/serialized and the session state is server-side only, my solution was to use a hidden field--easy enough, right? Well here's my problem... it seems as though it's working but the data isn't being propagated as I would've expected.

My expectation was this:

  1. Page is loaded for the first time. Server-side class recognizes that the hidden field is empty, initializes the container class, serializes the class to a JSON string and writes that value to the hidden field.
    • Page_Init: Unavailable.
    • Page_Load: Unavailable.
    • Page_LoadComplete: Available.
  2. Server processing completes, object is now available for use by client code.
  3. Object in hidden field is mutated by client code. Client code then fires a postback to the server (via a button).
  4. Server-side processing begins...
    • Page_Init: Unavailable.
    • Page_Load: Available, including client-side changes.
    • Page_LoadComplete: Available, including client-side changes.
  5. All is right in the world, a double-rainbow shines outside my window and a magical unicorn gives me a wink and a nod.

My observation is this:

  1. Page is loaded for the first time. Server-side class recognizes that the hidden field is empty, initializes the container class, serializes the class to a JSON string and writes that value to the hidden field.
    • Page_Init: Unavailable. (As expected)
    • Page_Load: Unavailable. (As expected)
    • Page_LoadComplete: Available. (As expected)
  2. Server processing completes, object is now available for use by client code.
  3. Object in hidden field is mutated by client code. Client code then fires a postback to the server (via a button).
  4. Server-side processing begins...
    • Page_Init: Unavailable. (As expected)
    • Page_Load: Available, but not updated with changes made on the client-side. (Unexpected).
    • Page_LoadComplete: Available, including client-side changes. (As expected)
  5. A dark cloud forms over my cubicle and I begin to contemplate whether or not my laptop would survive the second-story fall off the balcony.

Conclusion

This is causing me a bit of confusing for a couple reasons... the first is that I've never used the "LoadComplete" event before and can't seem to find any examples that suggest it's necessary to or even that it should be done that way. The second is that by the time load complete is raised, other events that rely on the data from the client side have already been fired.

Any help/explanation/suggestion; hell, even criticism is appreciated!

Thanks, Jason

Was it helpful?

Solution

Explaination
You can update HiddenField values in javascript and get them back at the server.
If you want your object to be available after Load, using LoadComplete is ok.
If you want this object to be available to all controls when they load, the earliest you can get the data from inputs is by overloading PreLoad and creating your object there.
There is no problem with your logic.

Conclusion
There is some bug in your implementation of it.
Lets take a look at the code now.

OTHER TIPS

I'm answering this in the hope that this helps save someone else a few hours. After much trial and finally success, I learned that you can get a HiddenField value during the OnInit event. Given a HiddenField with an ID of hidValue, the key line is:

string strValue = Request.Form[hidValue.UniqueID].ToString();

I've ripped a lot of hair on ASP.NET lifecycle :-). I would advise you this:

  • bind to control events
  • avoid binding or overriding to page events

In this case, you should have a protected HiddenField declared in your page/user control. So you really want to bind to the ValueChanged event, and forget about the rest.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top