I've got a user control that builds a table of dynamic data based on a :LINQ class holding the data. The data is loaded using the LoadData(DataInstance) method. The table it builds contains a number of dynamic controls that themselves have postback/autopostback so the display of the control needs to be built at latest in the Page.Load event or the event handlers for the controls don't get wired up.
Now if I have a page that uses this user control by passing the data in the page load, the control loads the data just fine. However, if I want to load the control with data as the result of a user made selection - say, a dropdownlist that has autopostback, it doesn't.
I understand why this is; on the postback, the page load event fires, which loads the control that doesn't yet contain any data because at this stage of the lifecycle the selection event handler hasn't yet been filed. Then the event handler fires which passes the data to the user control.
Now, one of two things happens depending on how I wire the control up. Either I have it reload the display, at which point, the event handlers are missing because they're required to be wired up in the page load, but I do have the data and the buttons displayed; OR, I don't reload control displaying the data at which point none of my controls have yet been loaded in the user control and consequently only the basic user control template is displayed.
So the question boils down to - what do I do about this? I found a couple of hack workarounds - i.e. reference the Page.Request.Params("__EVENTTARGET"). The only problem with this is that my user control loads a substantial amount of controls and having to parse the Request.Form object to figure out what was changed, and whether it's value field is called "VALUE", "TEXT", "SELECTEDVALUE" or some other named field... is a little laborious.
Any suggestions as to how to deal with this limitation of the page lifecycle? I'm wondering if there's a way that I can force the event handlers to be wired up in the Page PreRender event - even if I do this manually? If I can do this, then I can move the place the user control is built and still have the event handlers work - which is the way this *should* work in my opinion - but that's just me. If anyone's got any ideas, I'd welcome them...