Solved

Access Methods on Parent Page from User Control

Posted on 2006-07-18
5
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 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