Solved

Access Methods on Parent Page from User Control

Posted on 2006-07-18
5
301 Views
Last Modified: 2011-09-20
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?
0
Comment
Question by:tfountain
  • 4
5 Comments
 
LVL 24

Expert Comment

by:Justin_W
ID: 17134556
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.
0
 
LVL 24

Accepted Solution

by:
Justin_W earned 500 total points
ID: 17134568
You could also have your user control raise (or bubble) an event. Then you could simply raise the event from within the User Control, and rely upon each page to handle the event appropriately, meaning that each page would have an event handler for that event, and would call the page's own method when the handler is invoked.
0
 

Author Comment

by:tfountain
ID: 17141000
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.
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 17141021
You're welcome. And yes, reflection is a good thing to look into for future use.
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 17170813
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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
call function 8 40
asp.net mvc5 3 37
Code works but how can I download the file? 20 46
Calling stored proc in EDMX 11 19
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now