views:

47

answers:

2

I've got a custom control we'll call "TheGrid".

In TheGrid's controls is another control we'll call "GridMenu".

GridMenu has a button control in its own control collection.

I'd like to enable the developer using this control to associate a page level method with the OnClick of that button deep down inside GridMenu ala:

<customcontrols:TheGrid id="tehGridz" runat="server" onGridMenuButtonClick="mypagemethod" />
A: 

You can register an event handler for this event using delegates. See the following MSDN articles:

http://msdn.microsoft.com/en-us/library/system.eventhandler%28VS.71%29.aspx

http://msdn.microsoft.com/en-us/library/aa720047%28v=VS.71%29.aspx

Bernard
A: 

On the GridMenu (which I assume is another custom control), expose the event ButtonClick by declaring it as public:

public event EventHandler ButtonClick;

If you like, you can create a custom event handler by defining a delegate, and a custom event argument class. Somewhere in the logic of this control, you will need to raise the event (perhaps in the Clicked event handlers of buttons contained on GridMenu; events can cascade). Coding in C#, you'll need to check that the event is not null (meaning at least one handler is attached) before raising the event.

Now this event is visible to TheGrid, which contains your GridMenu. Now you need to create a "pass-through" to allow users of TheGrid to attach handlers without having to know about GridMenu. You can do this by specifying an event on TheGrid that resembles a property, and attaches and detaches handlers from the inner event:

public event EventHandler GridMenuButtonClick
{
   add{ GridMenu.ButtonClick += value;}
   remove { GridMenu.ButtonClick -= value;}
}

From the markup of a control containing a TheGrid control, you can now specify the event handler by attaching it to OnGridMenuButtonClicked the way you wanted.

KeithS
I had assumed something like this was the approach to take but no matter what I do I cannot get intellisence to recognize OnGridMenuButtonClicked.
D.Forrest
@D.Forrest: You must use GridMenuButtonClick instead of OnGridMenuButtonClicked
SKINDER
Sure I understood that. I'm actually calling my event "OnSearchClick". But it's definitely not visible via markup and breakpoints in debug on the add/remove declarations are not being hit.
D.Forrest
Well what SKINDER was saying is that ASP.NET generates an "On<your event name here>" wrapper behind the scenes that is used to attach handlers from the markup. When you set up your custom event as OnSearchClick, you have to specify OnOnSearchClick as the event you're attaching a handler to in the markup. Try changing the event you define in GridMenu's codebehind to "SearchClick"; that will allow you to attach handlers to OnSearchClick from the markup.
KeithS
Thanks Keith for the clarification. Mission accomplished.
D.Forrest