Go to DotNetFunda.com
 Welcome, Guest!  
LoginLogin  
Take a break and be productive! read technical jokes.
 Skip Navigation Links Home > Articles > ASP.NET > Creating/Setting properties of User Control in ASP.NET

All Articles | Submit Article |

Creating/Setting properties of User Control in ASP.NET

 Download source file
 Posted on: 10/15/2007 5:54:18 PM by SheoNarayan | Views: 4732 | Category: ASP.NET | Level: Intermediate | Print Article
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.

 Get Career Counseling  
DotNetFunda.Com brings you a FREE Career Counseling section where you can ask any type of career related question. Ask now!

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

Interesting?   Bookmark and Share


About Sheo Narayan

Experience:6 year(s)
Home page:http://sheonarayan.myfunda.net/
Member since:Tuesday, July 08, 2008
Biography:Throughout 1st in all educational exams.
Major qualifications: HDCS, ADCA, MCA, MCTS
Location: Hyderabad, India
 Latest post(s) from SheoNarayan

   ◘ Ensuring button is clicked only once, Confirmation and Please wait message for ASP.NET Form posted on 7/3/2009 3:34:15 PM
   ◘ Setting up Start Options in Visual Studio and Visual Web Developer posted on 6/28/2009 7:53:02 AM
   ◘ Website vs Web Application - Embedded resources posted on 6/23/2009 10:10:20 PM
   ◘ Adding removing xml attribute from XML File in .NET posted on 6/21/2009 12:55:43 AM
   ◘ Working with Themes in ASP.NET posted on 6/8/2009 12:13:10 PM


 Submit Article

About Us | Contact Us | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)