views:

167

answers:

1

I have a simple user control that contains some buttons that fire events which I want to be handled by the page using the control. I have my code setup as such:

public event EventHandler Cancel;
public event EventHandler Confirm;
public void Confirm_Click(object sender, EventArgs e)
{
    if (Confirm != null)
        Confirm(this, e);
}
public void Cancel_Click(object sender, EventArgs e)
{
    if (Cancel != null) 
        Cancel(this, e);
}

but when I try to call these from the page that is using the control's page load event I don't get any of the custom events

ASPX Code

<%@ Register TagPrefix="btg" TagName="CustomControl" Src="~/Search/CustomControl.ascx" %>
<btg:CustomControl ID="btgControl" runat="server" ></btg:CustomControl>

could this be because my buttons in the user control are within an update panel?

+1  A: 

You shouldn't be seeing methods. You should be seeing events.

In your parent page's load, you need to do this:

myUserControl.Cancel += new EventHandler(myUserControl_Cancel);

You can hit tab,tab to auto-generate the method stub. That will look like:

void myUserControl_Cancel(object sender, EventArgs e) {}

Then, this code will fire after it is called in the method of your user control. In order for that code to fire, you'll have to assign the events to button events on your user control.

edit: myUserControl is the id of your user control. Also, some would argue that event handlers should be in your page's init method.

edit:

Is your user control properly referenced in the page? i.e. Are you registering the user control in web.config or using the reference directive in the page?

Also, did you try cleaning the solution and rebuilding? If your user control is dynamically created/loaded, you'll have to wire up the events in the same scope as the instantiated control. In order to dynamically load the user control, you'll have to have a placeholder in your page and do the following:

UserControl control = Page.LoadControl("~/ControlPath/ControlName.ascx");
((MyUserControlClass)control).Cancel += += new EventHandler(myUserControl_Cancel); // etc...
Jim Schubert
this is what i'm saying, there are no events available to me in my parent page
Jimmy
Sorry, I assumed from your original post that you were trying to directly access the methods. Do you mean it's just not showing in intellisense? I updated my answer with a few more possibilities. Is it possible to post your aspx code where the control is added and the page_load?
Jim Schubert
I have rebuilt and cleaned my solution a couple of times, the control is being called properly and is loaded on the page when i browse it, I did have one mistake with using the user control's name instead of its ID on the page but I still am unable to access any of the events to delegate them
Jimmy
when i load the control via code behind I am able to see the events, maybe it is an issue with visual studio not locating my user control in the code
Jimmy
I am just going to avoid the headache of messing with the aspx code and do it from the code behind, thanks much
Jimmy
Since your buttons are in an updatepanel, you'll have to make sure they are posting correctly (async or postback) examples are here: http://msdn.microsoft.com/en-us/library/system.web.ui.asyncpostbacktrigger.aspx
Jim Schubert