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
Solved

Access Methods on Parent Page from User Control

Posted on 2006-07-18
5
304 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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