简体   繁体   中英

How do I give an ASP.Net custom control a new custom event

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.

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM