Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subscribe to event in user control

Status
Not open for further replies.

KDavie

Programmer
Feb 10, 2004
441
US
Although this questions pertains to a web project, it is c# specific.....

There is an event I need to subscribe to on a user control I'm working with. The user control has several of its own controls, one of which is a third-party treeview. I need to subscribe to the treeview's NodeClick event. The problem is that I need to use a base class to subscribe to the event instead of the page/pages that the user control is on. So basically the framework looks something like this....

User Control - Holds treeview
Web Page - Holds user control (Inherits Base Class)
Base Class - The Base Class for all web pages in the project (Inherits System.Web.UI.Page).

To further complicate things the base class is in a separate assembly and cannot reference the assembly that holds the user control and pages (this would cause circular dependency). I've tried to use Reflection and also tried using an Interface... Haven't had any luck with either... Currently the problem I am having is in the Base Class. I am trying to use System.Reflection.MethodInfo so I can create a delegate, but it requires the method to be static and making the method static makes it so MethodInfo can't find the method at all! This is driving me crazy... I've been working on it for a few days now... Any help would be greatly appreciated.

Currently, there isn't a lot of code since I recently wiped it out and started over... but here is the line that I am having issues with....

Code:
System.Reflection.MethodInfo mi = this.GetType().GetMethod("MyMethod",System.Reflection.BindingFlags.Static);

mi is coming back null. MyMethod is static. Also, when I make the method public instead of static the GetMethod command works but I need it to be static to create the delegate.

Kevin Davie
Consultant
Sogeti USA
 
I've resolved the EventInfo issue. Turns out that I was searching for the method out of its scope. However, even though I can now successfully create the delegate and add it as an event handler for the event, the method isn't firing... Here is the code (I'm just trying to handle the click event of a button in this example)....

Code:
[COLOR=green]//Get page controls[/color]
ControlCollection controls = Page.Controls;
[COLOR=green]//find user control within control collection[/color]
FindUserControl(controls, "TestEvent_ascx");
Control c = userControl;
[COLOR=green]//Get button from user control[/color]
Control button = c.Controls[0];
[COLOR=green]//Get click event info[/color]
EventInfo clickEvent = button.GetType().GetEvent("Click");
[COLOR=green]//Create reference to BasePage class[/color]
Type basePage = this.GetType().BaseType.BaseType;
[COLOR=green]//Get callback method info[/color]
MethodInfo staticCallback = basePage.GetMethod("StaticMethod", BindingFlags.Static | BindingFlags.Public);
[COLOR=green]//Create delegate with StaticMethod[/color]
Delegate d = Delegate.CreateDelegate(typeof(EventHandler), staticCallback);
[COLOR=green]//Add delegate to button click event handlers[/color]
clickEvent.AddEventHandler(button, d);

Any help is appreciated.

Thanks,

Kevin Davie
Consultant
Sogeti USA
 
Alright, I got it working... I was only adding the event listener on the initial load, once I started adding it in post-backs as well everything started working as expected:)

Kevin Davie
Consultant
Sogeti USA
 
don't forget to un-register the event when the client object is disposed.
simple setups would look like this
Code:
public override void Dispose()
{
   SomeObject.SomeEvent -= the_targe;
   base.Dispose();
}
I would assume your setup would look like this
Code:
public override void Dispose()
{
   clickEvent.RemoveEventHandler(button, d);
   base.Dispose();
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top