views:

94

answers:

4

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 info is needed, please ask and I'll provide it.

A: 

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.

JaredPar
+2  A: 

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
}
GenericTypeTea
Just to add eithout editing, you don't need to put the "if (this.UserCancelled!= null)" part in if you always use the event. If you're not always going to want to capture the event, then ensuring the delegate is not null prevents it causing an error.
GenericTypeTea
A: 

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.

DRapp
He's using ASP.Net.
GenericTypeTea
Yep, sorry for the confusion.
Rich
+1  A: 

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
    }
}
Kyle Chafin
This was a great reply as well, but was just beaten to the punch by GenericTypeTea's answer.
Rich
Heh, I was enjoying watching reputation ping-pong between me and Kyle. Thanks for accepting my answer in the end.
GenericTypeTea
Ha, yeah, I was trying to give you both credit. Thanks guys.
Rich