Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access Methods on Parent Page from User Control

Posted on 2006-07-18
5
Medium Priority
?
309 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 2000 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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
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…
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

971 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