top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+1 vote
1,117 views
How to access a user control from another user control in ASP.NET?
posted Feb 10, 2016 by Jayshree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

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
Name:
City:

5.Add a Label control in WebUserControl2.ascx

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


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.

answer Feb 10, 2016 by Shivaranjini
...