Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to access a user control from another user control in ASP.NET

Author: Rudy Pinson
Download Source Code : 1422_AccessUCFromAnotherUC.zip

I will show you in this article that how you can access a user control from another user control in ASP.NET.

 

 
Sometimes you need to access controls form a user control in another user control to use it for your purpose. Different techniques can be used to achieve this task. The famous and mostly used technique is the use of NamingContainer property of the control to access controls in a user control from another user control. NamingContainer property of the control gets a reference to server control’s naming container. So we can use NamingContainer property of child control to get reference to its parent container. FindControl() method is used to search the current naming container for a control for a specified ID.  We will use NamingContainer property and FindControl() method in a user control to access a Label control in a another user control.
 
 
  1. Create a new Web Site in Visual Studio 2010 either in C# or VB.NET
  2. Add a Web Form to Web Site
  3. Add two Web User Controls in the website
  4. Write code below in WebUserControl.ascx

    <table>
           <tr>
                  <td>Name:</td>
                  <td>
                         <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                  </td>
           </tr>
           <tr>
                  <td>City:</td>
                  <td>
                         <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                  </td>
           </tr>
           <tr>
                  <td colspan="2" align="center">
                         <asp:Button ID="btnSubmit" runat="server" Text="Submit"
                               onclick="btnSubmit_Click" style="height: 26px" />
                  </td>
           </tr>
    </table>
     
  5. Add a Label control in WebUserControl2.ascx

    <asp:Label ID="Label1" runat="server"></asp:Label>
     
  6. Now add references to both user controls in Default.aspx page by using @Register directive.

    <%@ Register Src="~/WebUserControl.ascx" TagName="UserControl1" TagPrefix="uc1" %>
    <%@ Register Src="~/WebUserControl2.ascx" TagName="UserControl2" TagPrefix="uc2" %>
     
    We need to set “Src”, TagName and TagPrefix properties of @Register directive for both User Controls. “Src” property should have the URL of the Web User control and you can set TagName and TagPrefix properties according to your wish but they should be meaningful.
     
  7. Write code below in Default.aspx page

    <div>
    <asp:Panel ID="Panel1" runat="server">
           <uc1:UserControl1 ID="UserControl1" runat="server" />
           <br />
           <uc2:UserControl2 ID="UserControl2" runat="server" />
    </asp:Panel>
    </div>

    Panel control is added to the page with “runat” property set to “server”. Both user controls are added inside Panel control using tag prefix and tag name. “runat” property is also added for both user controls.
     
  8. Now write code below in button click event of WebUserControl.ascx.cs or WebUserControl.ascx.vb

    C#
     
    protected void btnSubmit_Click(object sender, System.EventArgs e)
    {
           Panel myPanel = (Panel)btnSubmit.NamingContainer.NamingContainer.FindControl("Panel1");
           UserControl myUserControl2 = (UserControl)myPanel.FindControl("UserControl2");
           Label myLabel = (Label)myUserControl2.FindControl("Label1");
           myLabel.Text = "Your Name is: '" + txtName.Text + "' and you lives in: " + txtCity.Text;
    }
     
    VB.NET
     
    Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
           Dim myPanel As Panel = DirectCast(btnSubmit.NamingContainer.NamingContainer.FindControl("Panel1"), Panel)
           Dim myUserControl2 As UserControl = DirectCast(myPanel.FindControl("UserControl2"), UserControl)
           Dim myLabel As Label = DirectCast(myUserControl2.FindControl("Label1"), Label)
           myLabel.Text = "Your Name is: '" & txtName.Text & "' and you lives in: " & txtCity.Text
    End Sub
     
    NamingContainer property of Button control is used with FindControl() method to find the Panel control. FindControl() method is used again to find the User Control inside Panel control. FindControl() method is used once again to find Label control inside User Control. At the end, text in text box controls is assigned to Label control’s text property to display it.
     
  9. That’s it. View the website in browser and see the result.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to crop image using jQuery and Jcrop in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net