Click here to Skip to main content
6,595,444 members and growing! (22,357 online)
Email Password   helpLost your password?
Web Development » User Controls » General     Beginner License: The Code Project Open License (CPOL)

Access Parent Control's Method from a Child Control

By Moorthi N

Simple technique to access parent page and controls method from a child control
C#, Windows, WebForms, Dev
Posted:9 Jul 2008
Updated:20 Nov 2008
Views:13,956
Bookmarked:15 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 3.82 Rating: 3.67 out of 5
1 vote, 9.1%
1
1 vote, 9.1%
2
2 votes, 18.2%
3
1 vote, 9.1%
4
6 votes, 54.5%
5

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Moorthi N


Member

Occupation: Software Developer
Location: United States United States

Other popular User Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
GeneralExcellent artical Pinmemberdenii0:45 16 Sep '09  
GeneralMy vote of 2 PinmemberJoe Sonderegger4:27 25 Nov '08  
GeneralGood Job! PinmemberArielR0:13 25 Nov '08  
GeneralDifferent approach Pinmemberguohuang8:32 22 Nov '08  
Generaldifficult PinmemberEmiliana8517:43 12 Aug '08  
GeneralGood article. PinmemberRajib Ahmed6:26 9 Jul '08  
GeneralRe: Good article. PinmemberMoorthi N12:05 9 Jul '08  
GeneralWhy cast ? PinmvpChristian Graus5:23 9 Jul '08  
GeneralRe: Why cast ? PinmemberMoorthi N12:17 9 Jul '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Nov 2008
Editor: Deeksha Shenoy
Copyright 2008 by Moorthi N
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project