Go to DotNetFunda.com
  Welcome, Guest!  
LoginLogin  
{ Submit resources and get monthly gifts !!! }
Submit: Article | Interview Question | Joke | Question | Link || Search  
 Skip Navigation Links Home > Articles > Creating/Setting properties of User Control in ASP.NET

All Articles | Post Articles

Creating/Setting properties of User Control in ASP.NET

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

 
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

- Hide Code
  

<%@ 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


- Hide Code

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.

- Hide Code

<!-- 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.
- Hide Code


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
Bookmark and Share

About Sheo Narayan

Experience:6 year(s)
Home page:http://sheonarayan.blogspot.com/
Member since:Tuesday, July 08, 2008
Biography:--
Throughout first in the all educational exams.
Major qualifications: HDCS, BCA, ADCA, MCA
Locations: Hyderabad, India
 Latest post(s) from SheoNarayan

   ◘ How to pass a value from User Control to the Page posted on 7/8/2008 1:06:59 PM
   ◘ Implementing Custom Paging in ASP.NET with SQL Server 2005 posted on 6/25/2008 7:45:44 PM
   ◘ Checking / UnChecking all checkboxes of the page dynamically in a single click posted on 6/7/2008 7:51:55 AM
   ◘ Getting connectionStrings / appSettings values from web.config file posted on 6/3/2008 5:52:37 AM
   ◘ How to create an ASP.NET website using Visual Web Developer? (Pictorial View) posted on 5/23/2008 9:58:25 AM



Question: Why to use www.dotnetfunda.com google search?
Answer: This search has been especially optimized to search technical articles. You may find to-the-point results in comparison with other search.
Google
About Us | Contact Us | 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.