ASP.NET
  Home arrow ASP.NET arrow Page 2 - Reduce Repetitive Code with ASP.NET Custom...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Reduce Repetitive Code with ASP.NET Custom Controls
By: Rich Sbarro
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 18
    2003-01-08

    Table of Contents:
  • Reduce Repetitive Code with ASP.NET Custom Controls
  • Creating the Control
  • Databinding to the Control
  • Handling ViewState and Post Back Events
  • Implementing in an ASP.NET page
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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);
    }

    More ASP.NET Articles
    More By Rich Sbarro


     

    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







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway