Click here to Skip to main content
15,881,380 members
Articles / Web Development / ASP.NET

Access a UserControl From Another Page

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
21 Jan 2013CPOL2 min read 23.5K   7  
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.

WebUserControl1.ascx

ASP.NET
<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="WebUserControl1.ascx.cs" Inherits="WebUserControl1″ %>
<p>
    <b>WebUserControl 1</b></p>
    <asp:DropDownList ID=""DropDownList1″" runat="server" 
          AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem>Red</asp:ListItem>
        <asp:ListItem>Blue</asp:ListItem>
        <asp:ListItem>Green</asp:ListItem>
    </asp:DropDownList>

WebUserControl2.ascx

ASP.NET
<%@ 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)

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {
     ((Label)((this.Parent).FindControl("WebUserControl21″)).FindControl(
           "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.

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

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

IMyInterface.cs

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

Page1.aspx

XML
<div id = "wuc1″ runat ="server" 
style="width: 440px">
  <uc1:WebUserControl1 ID="WebUserControl11″ 
  runat="server" />
</div>
<div id = "wuc2″ runat ="server" 
style="height: 23px; width: 441px">
  <uc2:WebUserControl2 ID="WebUserControl21″ 
  runat="server" />
</div>

Page1.aspx.cs (Code Behind)

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

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

License

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


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

Comments and Discussions

 
-- There are no messages in this forum --