Click here to Skip to main content
Click here to Skip to main content

Access Parent Control's Method from a Child Control

, 20 Nov 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple technique to access parent page and controls method from a child control

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)

Share

About the Author

Moorthi N
Software Developer
United States United States
No Biography provided

Comments and Discussions

 
QuestionNice Article - Comments PinmemberMember 1116663220-Oct-14 2:32 
GeneralExcellent artical Pinmemberdenii16-Sep-09 0:45 
GeneralMy vote of 2 PinmemberJoe Sonderegger25-Nov-08 4:27 
GeneralGood Job! PinmemberArielR25-Nov-08 0:13 
GeneralDifferent approach Pinmemberguohuang22-Nov-08 8:32 
Generaldifficult PinmemberEmiliana8512-Aug-08 17:43 
GeneralGood article. PinmemberRajib Ahmed9-Jul-08 6:26 
GeneralRe: Good article. PinmemberMoorthi N9-Jul-08 12:05 
QuestionWhy cast ? PinmvpChristian Graus9-Jul-08 5:23 
AnswerRe: Why cast ? PinmemberMoorthi N9-Jul-08 12:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 20 Nov 2008
Article Copyright 2008 by Moorthi N
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid