Last post May 22, 2008 06:11 PM by geobud64
May 19, 2008 07:19 AM|sophie86|LINK
hi, i have a page which has a formview. I need to handle an event in a child control in the edit and insert templates. how can i do this: this is the situation: i have a control in a control. The parent control is on the formview.
This is the code:
child control:(contains a dropdownlist)
The parent control (which contains teh above child control)
The Formview Page: (contains the Parent control above)
SchoolDetails = (
RequireTerms(); ---->this method just sets a value to true/false on another control.....but this method isnt being called when the event occurs
basically, i want the event to be handled in the formview, but the event is fired in the child control, which is contained in the parent control, which is contained in the formview.....can anyone spot anything wrong above? thanks
May 22, 2008 06:11 PM|geobud64|LINK
You really should be using a event delegate this will help you track your event easier. Here is an overly simple expample of how event should be captured. You are taking the right approach and subscribing to the event in each of your classes. Is the parent
control capturing the event. Here's a way I know that works.
Let's start with the delegate. A delegate is a way of defining the signature of method you want to call when the event is triggered. For example take this code: (I always store this code in it's own cs file. It is not contained in a class. It just
sits on its own.)
Above we have declared a delegate called ButtonEventDelegate that will take two arguments. When you create your method that you want to call when the event is triggered, it must have those two arguments. The signatures must match.
Next in your form (class) you declare an event and attach it to that delegate. This is declared in a class refered to as the event publisher.
Now, here we go. Your button will subscribe to ButtonClick by doing this. This is called the subscriber
myCommandButton.Click += MyCalledMethod;
When your button is clicked it will call MyCalledMethod. Of cource be for it can compile or call that method we have to create it. "Do any of you methods match the signature of the delegate?" (childish but i think you get the point)
private void MyCalledMethod(object sender,
if (ButtonClicked !=
Please note the parameters in MyCalledMethod match the delegate (very important) and you see a little logic in there. The event is only fired if something subscribed to it
Those are the main pieces. Delegate events are of course most useful between classes so make sure you include a reference to the class containing the event declareation (public
event ButtonEventDelegate ButtonClick;) in your class that will fire the event and put the method that matches the signature.
Ok here's the big example:
In subscriber class: