views:

1624

answers:

4

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 ImageButtons. 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 PlaceHolders 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?

+1  A: 

I've had a similar issue that ended up being some code in the event handler that messed up the event getting re-wired to the buttons. I think it was that I was reloading the control after the click event had caused a change, but was recreating the controls' container, rather than just rebinding.

Hope that helps.

bdukes
A: 

Figured it out. In his answer, bdukes states:

I think it was that I was reloading the control after the click event had caused a change, but was recreating the controls' container, rather than just rebinding.

This was essentially the case here. I was calling the method which created the controls a second time, from methods called by the event handlers for said controls. This was recreating the controls, and that seemed to be causing postback confusion. I validated this with good old-fashioned Trace.

I got it cleared up, and now I just have a few other bugs to squash -- but those I can work out on my own. Thanks, bdukes!

John Rudy
A: 

Hi, I have, by looks, same problem as you guys. I recreate a control after each post back. My event is raised every second time. How do you rebind a control instead of creating a new instance? Sorry for the daft question.

source code is here: http://forums.asp.net/t/1382448.aspx

I appreciate your help.

regards

Wmin

I'd recommend asking a new question here on StackOverflow; you'll likely get more responses that way.
John Rudy
A: 

This behavior occurs because the user control changes name after the first postback.

If you overrides "ClientID" and "UniqueID" forcing both to return a predefined value, you can resolve this trouble (see link text)