Accessing a Method of aspx page into a User Control

Posted by in C# category on for Intermediate level | Points: 250 | Views : 22204 red flag
Rating: 3.8 out of 5  
 5 vote(s)

In this small article I am going to explain how to access a method of .aspx page into an UserControl.


Hi all,

In this small article iam going to explain how to process a method of our .aspx page into the user control and use it. For this intially add a web project. you will get Default.aspx page by default. Now right click the project solution and add a new item called WebUserControl.ascx.

Rename it as DemoWebUserControl.ascx

I will explain this using 3 steps :

Step 1:

   1) First Design your Default.aspx page by adding the path of the user control in this way   

<%@ Register TagPrefix="uc1" TagName="DemoUserControl" Src="~/DemoWebUserControl.ascx" %>

   2) Now add the respective User Control in this way in the same page


   <uc1:DemoUserControl ID="newVariable" runat="server">



   3) Now open Default.aspx.cs. Here we are going to add a simple method which returns a string that can be done in this way

public string message()


   return "DotNetFunda Is The Best Site For .Net Fundamentals !!!";


   4) Note that the class name for our file is _Default(This is default generated class name. For understanding purpose i am not changing it). This completes the work with Default.aspx page.


Step 2:

   1) Now open DemoWebUserControl.ascx page and add a label in this way


   Test :

   <asp:Label ID="lblTest" runat="server" Font-Bold="true" ForeColor="Red"></asp:Label>


   2) Now after adding the label we have to call the method in Default.aspx for that open DemoWebUserControl.ascx.cs. Add this following code

override protected void OnLoad(EventArgs e)


   // Casts the Page property to the desired type and access the method


   lblTest.Text = ((_Default)this.Page).message();


   3) Above mentioned code describes this would casts the Page property to the desired type so that we can access that method. The label text is assigned with the class name of parent page that is (_Default) and message is the mthod which we declared in the .aspx page

Step 3:

        This step is very simple just run the code now !!!! You will able to access the method from .aspx page and display  it in the user control in this way


By this way we can access the method of a .aspx page (content page) into a User Control.

Hope this helps !!!

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Radha Srikanth
Member Level: Silver
Member Status: Member,Moderator,MVP
Member Since: 10/22/2010 10:05:45 AM
Country: India
Thanks, Radha Srikanth

My self CH V R SRIKANTH having Professional Experience in Developing Desktop, Web based with ASP.NET, C#.NET, ADO.NET, C#, Jquery, Webservices, WCF and Objective C. I generally trade myself as an effective team player with proven strength in problem solving and strong analytical skills. Strong communication, interpersonal, learning and organizing skills matched with the ability to manage the stress, time and people effectively. Capable of working under pressure with willingness to lift sleeves up and get hands dirty on critical technical scenarios

Login to vote for this post.

Comments or Responses

Posted by: Stefanstaev on: 12/14/2010 | Points: 25
The user control must no know about page.
Posted by: Chvrsri on: 12/14/2010 | Points: 25
I didnt get but you please see step 1 point numbers 1,2 . There the page will be knowing the UserControl
Posted by: Karaniscool on: 12/17/2010 | Points: 25

But what happens when the method from the parent is having some class level objects such as Dataset. Will it be NULL?
One has to carefully use the method from the parent aspx.
Posted by: Jayakumars on: 6/14/2011 | Points: 25

Good post for Default did not include ascx page how will do this not working.
Posted by: CHVRSRI on: 6/14/2011 | Points: 25
hI Jayakumars,

Kindly look at Step 1 point numbers 1 and 2 , there i have clearly mentioned how to tag a user control to an .aspx page .

Kindly tell me if could help you in any other way.


Login to post response

Comment using Facebook(Author doesn't get notification)