Click here to Skip to main content
15,881,559 members
Articles / Programming Languages / C#
Article

Access Parent Control's Method from a Child Control

Rate me:
Please Sign up or sign in to vote.
4.06/5 (13 votes)
20 Nov 2008CPOL2 min read 72K   608   20   10
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.

C#
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:

C#
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)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generaldifficult Pin
Emiliana8512-Aug-08 16:43
Emiliana8512-Aug-08 16:43 

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

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