Home arrow ASP.NET arrow Page 3 - 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 - Databinding to the Control
(Page 3 of 6 )

Databinding is the ideal method for populating a control with data. When you databind a collection or a DataTable to a control, ASP.NET will enumerate through all of the child objects or rows and add them to the control.

The code below overrides the DataBind method of the base class to set up the drop down.

public override void DataBind()
{
if(_autoLoadData)
{
Contacts _contacts = new Contacts();
_contacts.LoadFromXmlFile(Page.Server.MapPath("Contacts.xml"));
base.DataSource = _contacts;
}

base.DataTextField = "Name";
base.DataValueField = "ID";
base.DataBind();

if(base.Items.Count == 0)
{
base.Items.Add( new ListItem("No contacts available.", "-1") );
}
}


The DataTextField and DataValueField properties tell the base class which properties of the Contact class to use when databinding.

In this instance, the Name property will be displayed in the contents of the drop down, while the ID property will serve as the value for each item in the list.

The AutoLoadData property is supplied to allow the developer to specify when the control should handle its own databinding.

You should be aware that if AutoLoadData is false, then you will need to manually supply a data source for the control.

While in most cases it may be useful for the control to databind automatically, if there are multiple instances of the same control on one page then it may be more efficient to do the databinding manually, rather than having each individual instance call the same data source.
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