Creating/Setting properties of User Control in ASP.NET

SheoNarayan
Posted by in ASP.NET category on for Intermediate level | Views : 106500 red flag
Rating: 5 out of 5  
 1 vote(s)

You might have came across a situation where you need to pass a value from your .aspx page to the user control (.ascx). This is possible in several ways including storing values into session or database and again retrieving at the user controls.

However, the easiest and optimized way is to create a properties of the user control and set it into the .aspx page and access it from the user control.


 Download source code for Creating/Setting properties of User Control in ASP.NET

Today, I am going to describe you a step-wise process of creating, setting and retrieving properties of the user control.

Picture - 1


Lets create a User Control (Person.ascx)

Below is the code for the User Control
  

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Person.ascx.cs" Inherits="UserControl_Person" %>
<div style="background-color: #eeffc4;border:1px solid #c0c0c0;">
<div style="margin: 10px;">
This is the User Control text.<hr />
First Name:
<asp:Label ID="lblFirstName" runat="server" Font-Bold="true"></asp:Label><br />
Last Name:
<asp:Label ID="lblLastName" runat="server" Font-Bold="true"></asp:Label><br />
Age:
<asp:Label ID="lblAge" runat="server" Font-Bold="true"></asp:Label>
</div>
</div>

In this code, I am placing three labels for First Name, Last Name and Age. I am going to assign values of these labels from either server side(aspx.cs) or .aspx page later on.

Now, here is the code behind for the user control (.ascx.cs) page


private string m_FirstName = string.Empty;
private string m_LastName = string.Empty;
private int m_Age = 0;

# region Properties
public string FirstName
{
get { return m_FirstName; }
set { m_FirstName = value; }
}
public string LastName
{
get { return m_LastName; }
set { m_LastName = value; }
}
public int Age
{
get { return m_Age; }
set { m_Age = value; }
}
#endregion Properties

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblFirstName.Text = m_FirstName;
lblLastName.Text = m_LastName;
lblAge.Text = m_Age.ToString();
}
}


In this page, I am going to declare three variables for First Name, Last Name and Age respectively and I have created properties for them. Here you have to create properties as if you are declaring properties in a class file (In fact User control is nothing but a class).

On Page_Load method, I am cheking for Not IsPostBack method and setting Label's text property to the respective variables.

Now lets create .aspx page (default.aspx)

In this page, I am going to call the user control we created above. Here is the code for this.

<!-- Call User Control here -->
<h3>Demo: Setting properties of user control from server side/.aspx page</h3>
<Demo:Person runat="Server" ID="Person1" FirstName="Sheo" LastName="Narayan" Age="12"/>


Picture - 2


You may have noticed in the above code that, I have set the properties of this user control here itself. You can do either way ie. either you specify User Control property into the .aspx page or into code behind (.aspx.cs) file. One interesting thing here is in both cases you will get the intellisense of the properties you have created into your user control.

Picture - 3


Below is the code for the codefile of the .aspx page.


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
SetProperties();
}

private void SetProperties()
{
Person1.FirstName = "Sheo";
Person1.LastName = "Narayan";
Person1.Age = 20;
}


Here, I have set the properties of the user control in Page_Load event after checking Not IsPostBack condition.

Now, you have everything. Just run it and you will see that how easily you are able to pass the values from your .aspx page to user control. If you have any problem, Just download the code, place into your root folder and run it. Is that simple?

Thanks

To pass value from User Control to the Page, see this article http://www.dotnetfunda.com/articles/article97.aspx
Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Posted by: Syedshakeer on: 9/2/2009
Hi sheo ,
This Article is very clear with code to understand....
Posted by: Psdotnetfunda on: 8/16/2011 | Points: 25
Thanks Sheo.This is very nice and simple article to understand.But I have a query.How we transfer the information from aspx page to user control in a button click event in stead of Page load.

Thanks
Prakash

Login to post response

Comment using Facebook(Author doesn't get notification)