views:

916

answers:

2

Say that I have a web user control that has several drop down lists in it. They are all set to AutoPostBack = true, BUT each SelectedIndexChanged event handler in my control will fire/chain the other SelectedIndexChanged handlers I have defined for the other DDLs. This means that when the user changes a single DDL, the event handlers are chained/fired for several other DDLs. The logic for which events are chained is very complicated, data driven, and can change depending on which list was actually changed by the user. Therefore, it is very difficult to determine which event handler would fire last.

From the page's point of view, I want to subscribe to a single SelectionChanged Event on the user control that will only fire one time per postback and not until all of the event handlers have fired. I don't care which event handlers have fired, only that the state of the control as a whole has changed.

I'm using C# 3.5/ASP.NET 2.0/VisualStudio 2008

How can I go about doing this?

EDIT: Moved clarification into original description. I think the fact that I specified AutoPostBack=true without specifying that chaining was happening was misleading. I apologize for the confusion.

+1  A: 

I think you need to create a delgate in child control and then reference that delegate into parent control.

@Jalpesh: Almost. You meant "event", not "delegate".
John Saunders
Wouldn't that cause the SelectionChanged event to fire multiple times from the page's point of view? Say 3 of the DDLs change. I only want the event to fire once from the page's point of view.
Daniel Auger
+2  A: 

It depends on when you need the event handler to fire in the page lifecycle.

Here's one strategy:

1) In your user control, track the selection changing of your dropdown lists. If the event handler is executed, update your local tracking variables.

2) In your usercontrol's PreRender handler, check your tracking variables and if called for, fire the user control's SelectionChangedEvent.

This strategy will guarantee that the event handling phase of the page lifecycle is done, but has the drawback that your main page won't receive the "SelectionChanged" on your user control until the PreRender phase. This may or may not work for your situation.

If you need to handle the SelectionChanged event for your usercontrol earlier, then you will likely have to put in more complicated tracking logic in your dropdownlist handlers, and add a tracking variable to ensure that the usercontrol's "SelectionChanged" event only ever gets fired once.

womp
This is definitely a possible solution and I like it. The only potential gotcha is if the page or composite controls that may encompass this control need to respond to such an event before pre-render.
Daniel Auger
Exactly. If you need it to go off earlier, you're probably looking at wading into your dropdownlist logic and determining when they've finished updating.
womp