Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29934 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Creating/Setting properties of User Control in ASP.NET

Creating/Setting properties of User Control in ASP.NET

1 vote(s)
Rating: 5 out of 5
Article posted by SheoNarayan on 10/15/2007 | Views: 37108 | Category: ASP.NET | Level: Intermediate red flag


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


 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

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Microsoft_MVP] [Administrator]
Biography:Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001.

Connect me on Facebook | Twitter | LinkedIn | Blog

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

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

Posted by: Psdotnetfunda | Posted on: 16 Aug 2011 02:37:54 AM | 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

>> Write Response - Respond to this post and get points
Related Posts

Many a time we come into the situation where we need to pass ProductIds or ItemIds separated by comma from UI to the database. The first thing that comes in our mind is its easy by using IN keyword in the SQL Server but this is not as easy as it looks like. In this article, I have shown how to handle this situation easily in the client side itself.

This FAQ is like a starter kit. It will help you understand the main aspects of Ajax in a rapid fashion....

You must have noticed one link in yahoo.com, msn.com or other popular websites named "Page Options". When you click this link you get a popup displaying different small several color icons. After clicking one of these icons your page theme changes without entire page refresh. Now you are able to see the same page with different look and feel.

Despite being a very useful feature, Themes are generally given low priority by the developers. In this article, I am trying to play with Themes and its different behavior. Hope we all will learn something from it.

.NET Remoting provides a way for application in different machines/domains to communicate with each other. To make an object remotable that should be inherited by MarshalByRefObject.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/28/2012 11:56:44 AM