Go to DotNetFunda.com
 Online : 355 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > Creating/Setting properties of User Control in ASP.NET

  • Nominate yourself for "ADO.NET" online training session tomorrow (13-Mar-2010) for FREE, click here.

  • Download OOPS and ASP.NET Online training session video and PPT from here.

Submit Article | Articles Home | Search 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: 8224 | 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.

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

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
If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Administrator]
Biography:Throughout 1st in all educational exams.
Major qualifications: HDCS, ADCA, MCA, MCTS
Developing and architecting applications in Microsoft technologies since year 2001.
Location: Hyderabad, India
 Latest post(s) from SheoNarayan

   ◘ Common operation with Files and Folders in ASP.NET posted on 9/4/2009 4:23:03 PM
   ◘ Working with CustomValidator control in ASP.NET posted on 8/19/2009 9:27:43 AM
   ◘ Backup and Restore database in ASP.NET posted on 8/7/2009 5:30:24 PM
   ◘ Passing data between layers using Generic list collection posted on 7/19/2009 7:28:40 AM
   ◘ jQuery and ASP.NET AJAX UpdatePanel posted on 7/17/2009 6:53:04 PM


 Responses
Posted by: Syedshakeer | Posted on: 02 Sep 2009 08:13:57 PM

Hi sheo ,
This Article is very clear with code to understand....

Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
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)