Yes, there is a similar question here. However, that question doesn't seem to have code attached to it (though it might have at some point; I see answers alluding to code), and the accepted answer (and other advice) on it aren't getting me where I need to be.
I have a fairly complex, and frankly not-well-designed VB.NET web app I'm extending with a new feature. The short form of the feature is that it allows administrative users to reposition certain elements on the home page. Redesigning for web parts is not an option at this stage.
The administrative page which allows the resizing uses simple user controls to represent each of the "boxes" on the home page which can be moved (forgive my ascii art):
+------------+
| Box Title |
+------------+
| /\ |
| < X > |
| \/ |
+------------+
The four arrows (and the center X -- or eye, depending on visibility of the box) are ImageButton
s. Each ImageButton
's Click
event is wired to code similar to the following:
RaiseEvent WidgetMoved(Me, New WidgetMovedEventArgs(WidgetMoveDirection.Up, widgetIDField.Value))
The hosting page (which is itself on a MasterPage
) initiates these user controls after binding to the data which determines where they are positioned:
For i as Integer = 0 To count
Dim widget As MyWidget = widgets(i) '' widgets is a collection
Dim box as controls_CustomizeWidget = BindWidget(WidgetColumns.Left, i, count, widget) '' This simply uses LoadControl and places the control in one of two placeholders
AddHandler box.WidgetMoved, AddressOf widget_WidgetMoved
AddHandler box.WidgetVisibleChanged, AddressOf widget_WidgetVisibleChanged
Next
The method with this code is called at every page load, whether or not I'm in a postback. (I "live-save" the data; you make a change, it saves to the database at that postback, then reloads.)
Every other postback, the user control button events do not fire. They initiate a new postback, and on this second one they fire fine. So, essentially, my controls require two clicks to get anything done.
Specifically, again, the controls are initialized in the Page.Load
event handler. I tried out PreInit
, but that didn't work because the rest of the page structure isn't built yet, and therefore trying to place them in their appropriate PlaceHolder
s fails with a NullReferenceException
. I've searched around the web, and not seen anything like this, and I have to admit that in every other scenario where I raise events from user controls (such as every page of this site), there are no problems. On the other hand, this is the only page on the site where a user control with events is dynamically loaded at run-time ...
I'm feeling pretty danged dumb at the moment. Any help getting the events to raise reliably, every time? What am I doing wrong?