tags:

views:

996

answers:

4

I feel like this is something that Google should be able to solve for me just fine, but I'm finding little to no examples that I really understand.

I have this custom control called UserControlTask. I wish to add an OnHide event to it and as the name implies, I want it to be thrown when the user control is being hidden.

I seem to find lot's of examples of overriding events like OnClick, but none where a totally new event is being added.

All I really know is that I need to declare the EventHandler...

public event EventHandler Hide;

Then I believe add the OnHide to the attributes in my CreateChildControls method. I think that is accurate anyway. Beyond this I know nothing.

A: 

You may be mixing up the methods that raise the events and the events themselves.

Control.OnClick is a method that raises the Control.Click event. In subclasses overriding OnClick (and calling base.OnClick) allows processing without needing to go through a handler.

On the other hand a control that composes has to use event directly.

Richard
+4  A: 

Your problem isn't adding the event (which is easy:

public event EventHandler Hide;

// this is the function that raises the event
private void OnHide() {
    if (Hide != null)
        Hide(this, new EventArgs());
}

Your problem is knowing when to call OnHide() (which raises the event) - since there is no way to know when the user hides the control (I assume you are talking about something that happens in the client side), there is no time in which you should raise this event.

configurator
Ok, so what about this. I learned that the control it inherits from has an OnHide. If I added it to the control like you have shown here, can I send the event from the control being inherited from to the derived control?
Carter
In that case just override the OnHide method.
Bryan
If the base has a *virtual* OnHide method, simply override it and your method will be called instead (make sure you call base.OnHide in your implementation). If the method is not virtual, perhaps you could find out what it does inside the OnLoad (Reflector can help) and see what you can do about it.
configurator
A: 

Two simple things really.

The first thing to do in your custom control is declare an event object:

public event System.EventHandler EmployeeSelected;

Then from there, in your code that you desire to raise the event with, you do the following:

protected void btnSelectEmployee_Click(object sender, EventArgs e)
{        
    if (!string.IsNullOrEmpty(txtSearchEmployeeId.Text)) 
    {
       EmployeeSelected(this, new EventArgs());
    }       
}

Then in your main calling class, you consume the event and process as needed.

There is also some added stuff you can do if you want to pass event arguments as well.

Note: I added some code from my own app. I figured it would be easy enough for you to extrapolate to your own app.

Dillie-O
A: 

This is just an addon to the solution. How to solves this problem in VB. Add the following to the control;

Delegate Sub OnCompleted()

Public Event Completed As OnCompleted

Raise the event in the control when needed

RaiseEvent Completed()

Then in the parent page you can cacth that event like this

<asp:somecontrol OnCompleted="SomeControl_OnCompleted"...

which is then handled in the code behind like this

Protected Sub SomeControl_OnCompleted() Handles SomeControl.Completed

easy peazy, just make sure that your event handler is the same as your delegate declaration.

Organ Grinding Monkey