In this article, we shall learn how to pass value from content page or master page to the user control.
Introduction
As per MSDN, A user control is a kind of composite control that works much like an ASP.NET Web page—you can add existing Web server controls and markup to a user control, and define properties and methods for the control. You can then embed them in ASP.NET Web pages, where they act as a unit.
In this article, let us learn how to pass value from content page or master page to the user control. To do that we shall create properties in the user control and try to access it and setting its value from content page or master page.
Get hundreds of .NET Tips and Tricks here, you can also attend online ASP.NET training here.
Lets assume that we have following code in ascx file (user control file).
ASCX PAGE (USER CONTROL)<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserControlProperty.ascx.cs"
Inherits="UserControlProperty" %>
<p><asp:Label ID="lblMessage" runat="server" ForeColor="Blue" EnableViewState="false"
/></p>
ASCX CODE BEHIND
public int CategoryId { get; set; }
public string CategoryName { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = "Category ID: " + CategoryId + "<br />" +
"Category Name: " + CategoryName;
}
In the above code snippet, we have a User control with CategoryId and CategoryName properties. On the Page_Load of the User control, we are retrieving its value and writing in the asp:Label control.
ASPX PAGE
<%@ Register Src="~/UserControlProperty.ascx" TagPrefix="uc1" TagName="MyControl" %>
In the .aspx page or master page, we have registered the user control so that we can use it. Now when we write the code on the aspx page, you will notice that we get the properties of the user control as intellisense as displayed in the picture below.
User control properties are being displayed in the intellisense
Below is my final code for calling the user control.
<uc1:MyControl id="myControl1" runat="server" CategoryId="5" CategoryName="Forums" />
On the .aspx page we have registered the User control using Register directive. When we try to use the User control, Visual Studio displays all the User control properties as the intellisense (as displayed in the picture above). Here we have set CategoryId as “5” and CategoryName property as “Forums” of the user control. Remember that we are setting these values from the content page. When we run the page, you can see that the values set from the content page are being written on the web page as in the User control's Page Load event, we are accessing these values and writing in the asp:Label control.
OUTPUT
Similarly, once you have registered the user control on the content page (.aspx) or master page (.master), you can access it in the code behind of these pages by using its id something like
myControl1.CategoryId = 10;
myControl1.CategoryName = "Articles";
Hope this article was useful. Thanks for reading.
Stay tuned for next article on User control.