Link to home
Start Free TrialLog in
Avatar of tfountain
tfountain

asked on

Access Methods on Parent Page from User Control

Simply put:  I have a user control that I use on multiple pages.  I want to access a given method in the code behind of the parent page containing the user control.  This method would have the same name on each parent page.
I am suing C# in ASP.NET 2.0.

More detail:
I have  a DataView on the Parent Page that lists employees and related data from a DB.  This is populate on page load via a method.  When you select an employee in the DataView a user control is unhidden(made visible) and then is populated with that employees information.  This information is then edited by the user and saved back to the DB from a button activated method within the user control.  Fthe user control is re-hidden.
How do I make the last step of the save operation refresh the data in the data view on the Parent Page?
Avatar of Justin_W
Justin_W

There are a couple of ways.

The easiest is to call the method(s) via Reflection. You simply need to get the (current) page's object instance, then use .NET's Reflection API to "Invoke" a method of that object instance with a specified name, signature, etc. As long as the method is declared as Public, you shouldn't have much trouble, but if it isn't public, you may need to run your app with higher-than-normal security privileges.

Another option would be to define an Interface that declares the method, then modify all of your pages so that they implement that interface. (Or you could simply define a common base class for all of the pages.) Then you would simply need to get the (current) page's object instance, typecast it to the appropriate interface/class type, and call the method on the typecasted object instance.
ASKER CERTIFIED SOLUTION
Avatar of Justin_W
Justin_W

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tfountain

ASKER

Justin W,
Thanks for the quick response.  I used your second suggestion as I was more familiar with raising events.  I will definitely look into reflection though.  Never played with it but it sounds like a good tool to use.
You're welcome. And yes, reflection is a good thing to look into for future use.
FYI: Here is a link to an article about "event bubbling" in User Controls for anyone that is interested:
http://www.odetocode.com/Articles/94.aspx