How to know when a UserControl has finished firing an Event?
we have a UserControl to handle User cancellations, that is used in a few places. This has a couple of input fields and a submission button. When they submit the User's status is updated and a few other things are done and a feedback message displayed.
On one of the pages which includes the control, after the User has successfully cancelled their submission via the UserControl we need the page to be notified somehow so it can call one of its methods and update its display [in this case, the User's status, which was attending and is now cancelled].
How do we link these up? I'd guess at something involving delegates and event handlers but don't have much experience with them so don't know if I'd be heading down a blind alley...
One very hacky solution would be for the UserControl to cause a re-direct and then have the page monitor the session or a query string parameter etc., but just typing it has made me shiver so would have to be very much a last resort.
If any more i开发者_JAVA百科nfo is needed, please ask and I'll provide it.
This should be pretty easy. Add a delegate event to your UserControl as follows:
public event EventHandler UserCancelled;
Then, in your user control at the end of the cancellation method, just call the delegate:
if (this.UserCancelled!= null)
{
this.UserCancelled(this, new EventArgs());
}
Then, just add a handler to the event on your user control's aspx markup:
OnUserCancelled="UserControl1_UserCancelled"
And finally, add a handler to your page:
protected void UserControl1_UserCancelled(object sender, EventArgs e)
{
// Your code
}
I think your instincts are correct. You could solve this by defining a custom Event and Delegate. Something along these lines should do:
public delegate void CancelledUserHandler();
public partial class UserCancellationControl : System.Web.UI.UserControl
{
public event CancelledUserHandler UserCancelled;
protected void CancelButtonClicked(object sender, EventArgs e)
{
// process the user's cancellation
// fire off an event notifying listeners that a user was cancelled
if (UserCancelled != null)
{
UserCancelled();
}
}
}
public partial class MyPage : System.Web.UI.Page
{
protected UserCancellationControl myControl;
protected void Page_Load(object sender, EventArgs e)
{
// hook up the ProcessCancelledUser method on this page
// to respond to cancellation events from the user control
myControl.UserCancelled += ProcessCancelledUser;
}
protected void ProcessCancelledUser()
{
// update the users status on the page
}
}
The easiest way is to create an event on the UserControl to signal that the cancelation has occured. Add a handler for that in the original form and update the display when it's fired.
If your form is a class of your own design such as
public class MyForm : Form
{
public void MyCustomRefresh()
{
}
}
Then, in your custom user control, I would assume its used on multiple forms to allow logging of cancel as you described... Then, in the end of your code of whatever event/button, you could do something like
((MyForm)this.FindForm()).MyCustomRefresh()
So you can use the "this.FindForm()" to get the form, type-cast to your custom form definition that you KNOW has such "MyCustomRefresh()" method and call it directly. No delegates needed.
精彩评论