Click here to Skip to main content
13,093,097 members (64,355 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 21 Jan 2013

Access a UserControl From Another Page

, 21 Jan 2013
Rate this:
Please Sign up or sign in to vote.
How to access a UserControl from another page

A few days ago, I was facing a problem where I had to access value of an ASCX Control’s controls and also its public method from another ASCX Control while both are on the same ASPX page.

The first scenario was like this: UserControl_1 has a DropDownList and UserControl_2 has a Label. When the SelectIndexChange is being raised from UserControl_1, I need to set SelectedItem’s value into the Label of UserControl_2.

And in the second scenario, the UserControl_2 will need to invoke a public method which is in UserControl_1.

I did some Googling and found out that using FindControl property would be the simpler way to solve the first scenario.

And as for the second scenario, there are a couple of good solutions out there. Like using Reflection or using Interface. Here, I will cover it using Interface.


<%@ Control Language=""C#"" AutoEventWireup="true" 

CodeFile="WebUserControl1.ascx.cs" Inherits="WebUserControl1″ %>
    <b>WebUserControl 1</b></p>
    <asp:DropDownList ID=""DropDownList1″" runat="server" 

          AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">






<%@ Control Language=""C#"" AutoEventWireup="true" CodeFile="WebUserControl2.ascx.cs" 

Inherits="WebUserControl2″ %>
<p><b>WebUserControl 2</b></p>
<asp:Label ID="Label1″ runat=""server"" Text="Label"></asp:Label>

<asp:Button ID="Button1″ runat=""server"" 

onclick="Button1_Click" Text="Button" />

WebUserControl1.ascx.cs (Code Behind of WebUserControl1.ascx)

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {
           "Label1″)).Text = this.DropDownList1.SelectedItem.Text;

This will do the job for the first scenario. The FindControl property takes an id as a parameter and also you will need to cast the Control which you are about to work with.

As for invoking a public method triggering from one to another will need the help of ASPX page which contains both ASCX.

Suppose we have this public method on WebUserControl1.ascx.cs.

public string GetTheText() {
    return "This text is from WebUserControl 1″;

Now, we need to call this method from WebUserControl2. First, create an Interface.


public interface IMyInterface {
   string GetMethod1();

The second implements this interface in your ASPX page where ‘GetMethod1()’ is only a via for WebUserControl2 triggering the public method of WebUserControl1.


<div id = "wuc1″ runat =""server"" 

style="width: 440px">
  <uc1:WebUserControl1 ID="WebUserControl11″ 
  runat=""server"" />
<div id = "wuc2″ runat =""server"" 

style="height: 23px; width: 441px">
  <uc2:WebUserControl2 ID="WebUserControl21″ 
  runat=""server"" />

Page1.aspx.cs (Code Behind)

public partial class WPage : System.Web.UI.Page, IMyInterface {
protected void Page_Load(object sender, EventArgs e) {
public string GetMethod1() {
  return this.WebUserControl11.GetTheText();

Last, on a Button_Click event of WebUserControl2, you can invoke the Page’s method which was implemented before is basically calling the WebUserControl1′s desired public method.

WebUserControl2.ascx.cs (Code Behind of WebUserControl2.ascx)

public partial class WebUserControl2 : System.Web.UI.UserControl {
    protected void Page_Load(object sender, EventArgs e) {
    protected void Button1_Click(object sender, EventArgs e) {
      IMyInterface iMI = this.Page as IMyInterface;
      if (iMI != null)
        Label1.Text = iMI.GetMethod1();sp; Label1.Text = iMI.GetMethod1();

Another solution is to make a base class and make a virtual method. You can then use this as a base for your page and override the method. The ASCX has then no problem calling via the base either. I find this solution handy too.


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


About the Author

Md Nazmoon Noor
Software Developer (Senior)
Bangladesh Bangladesh
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170813.1 | Last Updated 21 Jan 2013
Article Copyright 2013 by Md Nazmoon Noor
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid