AspNet4YouTop 10sDevelopers WorldForums
Home
About
ASPNet Books
ASPNet Sites
ASPNet Hosters
View Articles
Search Articles
Authors
View Forums
 
Quick Menus
HOME
About
AspNet4You Forums
Articles
Authors
Search
Articles Summary
Articles(RSSFeed)
AspNet Books
AspNet Sites
AspNet Hosters
 Top 10 ASP.NET Books 
Professional ASP.NET 1.1
Essential ASP.NET With Examples in C#
ASP.NET Unleashed
Programming Data Driven Web Applications with ASP.NET
Professional ASP.NET Web Services
Beginning ASP.NET 1.1 with Visual C# .NET 2003
Programming Microsoft ASP.NET
Beginning ASP.NET Databases Using VB.NET
ASP.NET Security
Developing Microsoft ASP.NET Server Controls and Components
More...
 Top 10 ASP.NET Hosters 
WebHost4Life
DiscountASP.NET
MaximumASP
Brinkster
ORCS Web
myhosting.com
ISQSolutions
ASPwebhosting.com, LLC
Active ISP
Aquest Hosting
More...
 Top 10 ASP.NET Sites 
Asp.Net
GotDotNet
4GuysFromRolla.com
123aspx.com
EggHeadCafe.com
CShrp.Net
.NET 247
DevelopersDex.com
Csharp-Corner.com
dotnetspider
More...
Search Articles
Google
ASPNET4YOU      
Category:   Search Type:   Match Type:  
How to Access the Default Attribute of a User Control?
Author: Saha, ProdipPosted: 3/30/2005 8:27:04 PM

How to Access the Default Attribute of a User Control? 

When we create a custom control with some public properties we normally assign a value to the DefaultValue attribute on the Property. One would expect this default value is set if there is no value assigned to the Property from the calling page. Unfortunately, it does not work that way. You must take extra step to check if the value is assigned from the calling page. If not, you can access the DefaultValue attribute and assign it to the Property.

Here is an example of a Property called Location (string type) and it has a Defaultvalue attribute assigned with "mydefaultvalue". This article will show you how to set the Location property if it's not already assigned a value.

[Bindable(true), Category("Behaviour"), DefaultValue("mydefaultvalue")] 
public string Location  
{ 
	get 
	{ 
		return _Location; 
	} 

	set 
	{ 
		_Location = value; 
	} 
}
					

The attributes are residing in a AttributeCollection Class in System.ComponentModel namespace. Our goal is to find the DefaultValue attribute declared on Location property from the AttributeCollection Class. The following function will retrive the value from the AttributeCollection and assign it to the Location property-

public void SetLocation()
{
	try
	{
		//Check if value is already assigned or not
		if(this.Location==String.Empty)
		{
			//this=user control itself
			// Gets the attributes for the property.
			System.ComponentModel.AttributeCollection attributes = TypeDescriptor.GetProperties(this)["Location"].Attributes;

			//Set the default values if values are not assigned
			DefaultValueAttribute myAttribute = (DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
			this.Location=myAttribute.Value.ToString();
		}
	}
	catch(Exception ex)
	{
		throw ex;
	}
}
					

Prodip K. Saha
The Architect of WWW.ASPNET4YOU.COM.

Terms and Conditions