How To Access One UserControl from Another UserControl Using ASP.NET

Few days ago, I need to access one UserControlfrom to another UserControl.The scenario is like,When I press a button it will assign a value from UserControl-1's TextBox to UserControl-2's TextBox. I have developed it but I made a silly mistake so that it was not working.

Now this is the time turn to google......:). Fortunately I have got almost similar solution from KaushaL PariK(MVP). Very simple.....from his solution I found out my place this here for you guys....hope that you enjoy it.

Some known tips

Everybody knows the purpose of FindControl (Control.FindControl Method (String) (System.Web.UI)) method; it searches the current naming container for a server control with the specified id parameter.

In addition, there is one property called NamingContainer (Control.NamingContainer Property (System.Web.UI)) which Gets a reference to the server control's naming container, that means it will give the parent container reference of current server control's.

We need to set the Text property of txtTransfer from UserControl-1 with the value of UserControl-2's TextBox txtBalance.

The idea to accomplish this; is, to get the parent control reference (using NamingContainer, for Ex., txtTransfer.NamingContainer will give the reference of its container UserControl - 1) of TextBox; that is UserControl - 1, again get the parent control reference of the UserControl - 1; that is _Page.

As now we got the reference of the Current Page, we can now easily find the second user control's TextBox txtBalance with findcontrol method.

Below is the Code:


This UserControl1 just contains a simple TextBox may having some value...provides by user..this value is transfer to txtBalance TextBox of UserControl-2.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.cs" Inherits="WebUserControl" %>
<asp:TextBox ID="txtTransfer" runat="server" %> </asp:TextBox%>

A Simple One Line of code will access txtBalance TextBox of UserControl-2 on the page and set the Text Property of that TextBox with the value of UserControl-1's TextBox txtTransfer:

protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack) { }

protected void btnTransfer_Click(object sender, EventArgs e)
//reaching to get the _Page reference with NamingContainer Property and using FindControl method to get the TextBox
//txtTransfer.NamingContainer will give reference of the UserControl1's instance reference on the page
//txtTransfer.NamingContainer.NamingContainer will give reference of the _Page's reference
//Using FindControl Method to find UserControl - Control2
//again using FindControl Method to find TextBox inside UserControl - Control2
((TextBox)((UserControl)((Panel)txtTransfer.NamingContainer.NamingContainer.FindControl("Panel1")).FindControl("Control2")).FindControl("txtBalance")).Text = txtTransfer.Text;

WebUserControl2.ASCX (No Code in Code Behind):

Simple defining a TextBox:

<%@Control Language="C#" AutoEventWireup="true" CodeFile= "WebUserControl2.ascx.cs" Inherits ="WebUserControl2" %>
<asp:TextBox ID="txtTransfer" runat="server" ></asp:TextBox>

Finally Default.ASPX Page (No Code in Code Behind):

Placing both the controls inside a Panel Server Container control:

<%@Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@Register Src="~/WebUserControl1.ascx" TagPrefix="kk1" TagName="Control1" %>
<%@Register Src="~/WebUserControl2.ascx" TagPrefix="kk2" TagName="Control2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
<head runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server">
<div><kk1:Control1 ID="Control" runat="Server" /></div>
<div><kk2:Control2 ID="Control2" runat="Server" /></div>

In, above example, you can see that with the benefit and use of NamingContainer Property; there doesn't require to take the reference of UserControl2 in UserControl1 to access TextBox inside. Hope that it may helps other developers.


abhilash said…
please check the webusercontrol is ASPX or ASCX
abhilash said…
Please check the example which you provide, the usercontrol is not aspx its ascx
Ahsan Murshed said…
Thanks abhilash for you correction

Popular posts from this blog

How can I get a value of a property from an anonymous type (C#)

The model backing the 'MyDBContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

"This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."