How To Access One UserControl from Another Using ASP.NET
Recently, I came across a post in the asp.net forums, where a user was trying to access the value of a dropdownlist kept in one UserControl, from another UserControl. There were quiet a number of suggestions given, however either they were too complex or not explained well. In this article, we will see how easy it is to achieve this requirement.
Step 1: Create a new ASP.NET website. Right click the project > Add New Item > Web User Control. Give it the name ‘ControlA’. Similarly create another user control and give it the name ‘ControlB’.
Step 2: We will now create a dropdownlist(ddlItems) in ‘ControlB’ and access the selected value of the dropdownlist in 'ControlA’.
To do so, drag and drop a dropdownlist in the usercontrol ‘ControlB’ and add some items to it. The source will look similar to the following:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlB.ascx.cs" Inherits="ControlB" %>
<asp:DropDownList ID="ddlItems" runat="server">
<asp:ListItem>Item A</asp:ListItem>
<asp:ListItem>Item B</asp:ListItem>
<asp:ListItem>Item C</asp:ListItem>
</asp:DropDownList>
Also expose the dropdownlist as a property as shown below:
C#
public partial class ControlB : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public DropDownList ControlB_DDL
{
get
{
return this.ddlItems;
}
}
}
VB.NET
Public Partial Class ControlB
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Public ReadOnly Property ControlB_DDL() As DropDownList
Get
Return Me.ddlItems
End Get
End Property
End Class
Step 3: Now drag and drop a TextBox(txtDDLValue) and a Button(btnDDLValue) on to the usercontrol ‘ControlA’.
Another crucial part is to add a Reference directive of ‘ControlB’ in ‘ControlA’. Since the user controls are compiled into assemblies, adding a reference enables ‘ControlA’ to access the methods and properties of ‘ControlB’. The source will look similar to the following:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlA.ascx.cs" Inherits="ControlA" %>
<%@ Reference VirtualPath="~/ControlB.ascx" %>
<asp:TextBox ID="txtDDLValue" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnDDLValue" runat="server" OnClick="btnDDLValue_Click" Text="Get DropDown Value" />
Add the following code to the codebehind of ‘ControlA’
C#
public partial class ControlA : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnDDLValue_Click(object sender, EventArgs e)
{
ControlB ctrlB = (ControlB)Page.FindControl("cB");
DropDownList ddl = ctrlB.ControlB_DDL;
txtDDLValue.Text = ddl.SelectedValue;
}
}
VB.NET
Public Partial Class ControlA
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub btnDDLValue_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim ctrlB As ControlB = CType(Page.FindControl("cB"), ControlB)
Dim ddl As DropDownList = ctrlB.ControlB_DDL
txtDDLValue.Text = ddl.SelectedValue
End Sub
End Class
Since we have referenced ‘ControlB’ in ‘ControlA’, we can find the control and access the dropdownlist, that was exposed as a property in Step 2.
Step 4: All you have to do now is to add the user controls on to the page(default.aspx). To do so, add the Register directive to register both the user controls and then use the usercontrols. The source will look similar to the following:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="~/ControlA.ascx" TagName="ControlA" TagPrefix="ctrlA" %>
<%@ Register Src="~/ControlB.ascx" TagName="ControlB" TagPrefix="ctrlB" %>
<!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>Pass Message From One UserControl To Another</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ctrlA:ControlA id="cA" runat="server" />
<br />
<br />
<br />
<ctrlB:ControlB ID="cB" runat="server" />
</div>
</form>
</body>
</html>
Run the application. Change the value of the dropdownlist in ‘ControlB’. Now click the button in ‘ControlA’ and the selected value of the dropdownlist will appear in the textbox.
That was quiet simple. Your requirement may not be as simple as the one demonstrated in this article, however the basics of accessing one user control from another usercontrol will remain the same. I hope this article was useful and I thank you for viewing it.
Give me a +1 if you think it was a good article. Thanks!