Click here to Skip to main content
Email Password   helpLost your password?

Introduction

This article is for the beginner level audience who has just started working on user controls. Sometimes while using user controls, you may need to access methods declared either on the parent user control and parent page from the child user control. Here is a simple solution for that.

Background

I came across the same issue where I needed to access the method declared on the parent user control from the child user control and ended up searching for options. I got a simple solution using abstract classes as it was not possible to access the user defined methods in the Parent object.

Here is the little trick. I have created  abstract class AbstractUserControl and AbstractPage, where AbstractUserControl is inherited from System.web.UI.UserControl and AbstractPage is inherited from System.Web.UI.Page. I placed these two classes inside the App_code folder so that it will be accessible anywhere in the site.

Using the Code

Here is the abstract class with the declaration of abstract methods. These abstract methods will be overridden in the Page or UserControl where they need to be called from the child.

namespace CodeProject.Sample
{
public abstract class AbstractPage : System.Web.UI.Page 
{
public AbstractPage()
{
//
// TODO: Add constructor logic here
//
}

// implementation in the concrete class
public abstract void callMefromChild();
}
}

I added a similar abstract class for a user control to inherit.

Next, I use these abstract classes to achieve the functionality. Now it is pretty straight forward to access the parent page method from the child control. Here is the snippet:

protected void btnClick_Onclick(Object sender, EventArgs e)
{ 
   ((AbstractPage)Parent.Page).callMefromChild(); 
}

Simple type casting of Parent.Page to the AbstractPage classes exposes the user defined method in the Page. We can use the same trick for accessing the method declared on the parent user control from the child user control.

I am a long time user of The Code Project and never contributed to the community. Today I am starting with this simple article and expecting to come up with some interesting stuff later on. Hope this helps somebody. Please free to post a comment if you need any clarifications.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralExcellent artical
denii
0:45 16 Sep '09  
I was looking all around for almost half a year and found here.
Big thanks.
GeneralMy vote of 2
Joe Sonderegger
4:27 25 Nov '08  
Bad practice
GeneralGood Job!
ArielR
0:13 25 Nov '08  
Have you an example code files in c#?
GeneralDifferent approach
guohuang
8:32 22 Nov '08  
I'd do it differently, it is better to create a public event on the user control, and raise that event on the button click event and then have the page to pick up the event and do whatever you want to do there.

example (pseudo code)
User control
public event eventhandler ButtonClicked;

protected void btnClick_Onclick(Object sender, EventArgs e)
{
//raise the event
ButtonClicked(sender,e);
}

Page
Pick up the event do something
void usercontrol_ButtonClicked(Object sender, EventArgs e)
{
callMefromChild();
}
Generaldifficult
Emiliana85
17:43 12 Aug '08  
It's a little difficult for me. I currently use Children Control. It works pretty fine for controlling children's access to PC.
GeneralGood article.
Rajib Ahmed
6:26 9 Jul '08  
Good information. Should be more informative about when you may need to do this. Stories help. Thanks for the good contribution.


GeneralRe: Good article.
Moorthi N
12:05 9 Jul '08  
Thanks for your inputs.. will try to write the new articles more informatively.
GeneralWhy cast ?
Christian Graus
5:23 9 Jul '08  
How would you need to cast - if you don't make the base page your derived class in the first place, that won't work, and if you do, no cast is needed, right ?

I *always* create my own intermediate base class for all pages, and a seperate one for secure pages, so I can add things like this.

Christian Graus

No longer a Microsoft MVP, but still happy to answer your questions.

GeneralRe: Why cast ?
Moorthi N
12:17 9 Jul '08  
Keeping an own intermediate base class always for all pages is a good option.

about your Q, Nor sure i got your point correctly.

From what i understood, If i dun make the base page my derived class, Parent.Page object wont offer me the method i wanted to execute on the page.


Last Updated 20 Nov 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010