Events in ASP.NET UserControl raise every other click?

Yes, there is a similar question here . However, this question doesn't seem to be tied to it (although it might be at some point, I see answers referencing the code) and the accepted answer (and other guidelines) to it doesn't give me where I need to to be.

I have a rather complex and frankly not-well-developed VB.NET web application that I am extending with a new feature. The short form of the function is that it allows administrative users to move certain items on the home page. Refactoring for web parts is not an option at this stage.

The admin page, which is resizable, uses simple custom controls to represent each of the "boxes" on the master page that can be moved (pardon my ascii art):

+------------+
| Box Title  |
+------------+
|     /\     |
|   < X  >   |
|     \/     |
+------------+

      

The four arrows (and the center X - or eye, depending on the visibility of the window) - ImageButton

s. Each event ImageButton

Click

connects to code similar to the following:

RaiseEvent WidgetMoved(Me, New WidgetMovedEventArgs(WidgetMoveDirection.Up, widgetIDField.Value))

      

The hosting page (which itself is on MasterPage

) initiates these custom controls after being bound to the data that defines where they are located:

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 every time the page is loaded, whether I am enabled or not. (I "save" the data, you make changes, it is persisted to the database on this postback, and then reloaded.)

In any other postback, the custom control buttons will not work. They initiate a new relay, and on this second they work fine. So, in essence, my controls take two clicks to do something.

Specifically, again the controls are initialized in the event handler Page.Load

. I tried PreInit

it but it didn't work because the rest of the page structure hasn't been built yet and so trying to place them in the appropriate PlaceHolder

one fails with NullReferenceException

. I've searched the web and haven't seen anything like it, and I have to admit that every other scenario where I raise events from custom controls (for example, on every page of this site) is no problem. On the other hand, this is the only page on the site where a custom event control is dynamically loaded at runtime ...

At the moment I feel pretty stupid. Any help getting events to improve reliability, every time? What am I doing wrong?

+1


source to share


3 answers


I had a similar issue that ended up with some code in the event handler that messed up the event that connected to the buttons again. I think it was that I was reloading the control after the click event triggered the change, but it was recreating the container of the controls, not just restoring.



Hope it helps.

+1


source


Found it out. In response, bdukes states:

I think I was reloading the control after the click event triggered the change, but recreated the control container, not just redoing it.



It was mostly here. I called the method that created the controls a second time from the methods called by the event handlers for the specified controls. It was a recreation of the controls, and it seemed to cause confusion after feedback. I confirmed this with the good old fashioned track.

I figured it out and now I just have a few other squash errors, but I can work on my own. Thank bdukes

+1


source


This is because the custom control changes its name after the first postback.

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

+1


source







All Articles