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

Recently, I came across a post in the asp.net forums (This one > http://forums.asp.net/t/1313476.aspx) , where a user was trying to get the value of a dropdownlist's selected value kept in one UserControl, and set this value as text of one label which is from another UserControl. I have given the answer and giving here some of the description about the same post.

The Scenario is like; there is one UserControl - 1 in which there is one DropDownList. There is another UserControl - 2 in which there is one Label. In DropDownList's SelectedIndexChanged event; we need to set the selected value in Label which resied in UserControl - 2.

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.



Tha above image is showing the exact scenario which is discussed above. We need to set the Text property of Label with the selected value of DropDownList.
The idea to accomplish this; is, to get the parent control reference (using NamingContainer, for Ex., DropDownList1.NamingContainer will give the reference of its container UserControl - 1) of DropDownList; 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 Label with findcontrol method. Hope it makes sense :-)

Below is the Code:

WebUserControl1.ASPX:
A Simple UserControl with DropDownlList with some Static ListItems having AutoPostBack = True:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.cs"
    Inherits="WebUserControl" %>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    <asp:ListItem>Select</asp:ListItem>
    <asp:ListItem>test1</asp:ListItem>
    <asp:ListItem>test2</asp:ListItem>
    <asp:ListItem>test3</asp:ListItem>
    <asp:ListItem>test5</asp:ListItem>
</asp:DropDownList>

WebUserControl1.ASPX.CS:
A Simple One Line of code will access Label of Second UserControl on the page and set the Text Property of that label with Selected Text of this DropDownList:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) { }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //reaching to get the _Page reference with NamingContainer Property and using FindControl method to get the Label
        //DropDownList1.NamingContainer will give reference of the UserControl1's instance reference on the page
        //DropDownList1.NamingContainer.NamingContainer will give reference of the _Page's reference
        //Using FindControl Method to find UserControl - Control2
        //again using FindControl Method to find Label inside UserControl - Control2
        ((Label)((UserControl)((Panel)DropDownList1.NamingContainer.NamingContainer.FindControl("Panel1")).FindControl("Control2")).FindControl("Label1")).Text = DropDownList1.SelectedItem.Text;
    }

WebUserControl2.ASPX (No Code in Code Behind):
Simple defining a Label:

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

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <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>
        </asp:Panel>
    </form>
</body>
</html>

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 Label inside. Thats it!

Comments

# How To Access One UserControl from Another UserControl Using ASP.NET &laquo; KaushaL.NET

Pingback from  How To Access One UserControl from Another UserControl Using ASP.NET « KaushaL.NET

# reference one custom user control from another | keyongtech

Pingback from  reference one custom user control from another | keyongtech

# ???????????????????????????????????????????????????????????????????????? - .net???????????? - ????????????????????????????????????????????????????????? ???????????? .NET?????? ASP.NET - 123Doing

Pingback from  ???????????????????????????????????????????????????????????????????????? - .net???????????? - ????????????????????????????????????????????????????????? ???????????? .NET?????? ASP.NET - 123Doing

# ???????????????????????????????????????????????????????????????????????? - .net???????????? - [??????:tags] - ????????? | 123Doing

Pingback from  ???????????????????????????????????????????????????????????????????????? - .net???????????? - [??????:tags] - ????????? | 123Doing

# How to find a user control in another user control of service control - .net - How to find a user control in another user control of service control - Open Source FAQ System

Pingback from  How to find a user control in another user control of service control - .net - How to find a user control in another user control of service control - Open Source FAQ System

# Label content in custom UserControl not updating | Q Sites

Pingback from  Label content in custom UserControl not updating | Q Sites