Home arrow ASP.NET arrow Page 2 - Reduce Repetitive Code with ASP.NET Custom Controls
ASP.NET

Reduce Repetitive Code with ASP.NET Custom Controls


In this article Rich explores the world of ASP.NET Custom Controls and how they can dramatically simplify development of ASP.NET web applications.

Author Info:
By: Rich Sbarro
Rating: 5 stars5 stars5 stars5 stars5 stars / 57
January 08, 2003
TABLE OF CONTENTS:
  1. · Reduce Repetitive Code with ASP.NET Custom Controls
  2. · Creating the Control
  3. · Databinding to the Control
  4. · Handling ViewState and Post Back Events
  5. · Implementing in an ASP.NET page
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Reduce Repetitive Code with ASP.NET Custom Controls - Creating the Control
(Page 2 of 6 )

Imagine that you are writing an ASP.NET application to manage contact information. One control that would be useful in such an application would be a standard "Contacts" dropdown.

For this example, assume that you have a Contact class with properties such as Contact ID, First Name, Last Name, Address, City, State and ZIP.

The Contact class also has a read-only property called Name that returns a concatenation of Last Name comma First Name.

Additionally, you have a Contacts class that inherits from the CollectionBase class and holds a collection of Contact objects.

To create your custom Contacts web control, first create a class called ContactsDropDownList that inherits from the DropDownList class.

You’ll need to create the following properties as well:

NameTypeDescription
SelectedContactIDInt32Read-only. Returns the selected Contact ID.
SelectedContactNameStringRead-only.Returns the selected Contact Name.
AutoLoadDataBooleanDetermines whether the control should perform its own data access. If
false, then the DataSource must be supplied by the developer.


The C# code for the class with the properties defined is shown below.

using System;
using System.Web;
using System.Web.UI.WebControls;

namespace WebControls
{
public class ContactsDropDownList : DropDownList
{
private bool _autoLoadData = true;
public bool AutoLoadData
{
get { return _autoLoadData; }
set { _autoLoadData = value; }
}

public int SelectedContactID
{
get { return int.Parse(this.SelectedItem.Value); }
}

public string SelectedContactName
{
get { return this.SelectedItem.Text; }
}
}
}


This class will also need a constructor, and in the constructor you can set the properties of the base class so that every instance of this control will have the same look and feel.

By setting display properties in the constructor, you also allow a developer to change these values when they declare the control in an ASP.NET page.

Use the code below to set the CssClass and Width properties:

public ContactsDropDownList()
{
base.CssClass = "text-default";
base.Width = new System.Web.UI.WebControls.Unit(250);
}

blog comments powered by Disqus
ASP.NET ARTICLES

- How Caching Means More Ca-ching, Part 2
- How Caching Means More Ca-ching, Part 1
- Reading a Delimited File Using ASP.Net and V...
- What is .Net and Where is ASP.NET?
- An Object Driven Interface with .Net
- Create Your Own Guestbook In ASP.NET
- HTTP File Download Without User Interaction ...
- Dynamically Using Methods in ASP.NET
- Changing the Page Size Interactively in a Da...
- XML Serialization in ASP.NET
- Using Objects in ASP.NET: Part 1/2
- IE Web Controls in VB.NET
- Class Frameworks in VB .NET
- Cryptographic Objects in C#: Part 1
- Sample Chapter: Pure ASP.Net

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials