Custom Data Binding for Server Controls
page 3 of 7
by Justin Lovell
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 35722/ 82

Pretty much down hill from here

There is just one thing that must be considered for the data binding. That is to extract the data from the objects that you may receive from the collection. This can be simply done by falling back onto the famous DataBinder class:

 

DataBinder.Eval(obj, "ColumnName");

 

Depending on the type of object (“obj” in the above code example), it will determine the amount of reflection that will occur to get the value. If it is a DataRow class or DataRowView class (from the DataTable and DataView, respectively, the less reflection that will occur; however, for any other object, more reflection will be involved to get the value from the property (the name of the property is determined by the “ColumnName” parameter).

 

With all that said, you should deal with the data that will be used. For example, your control only needs to deal with two columns and a data reader is issued to us with ten columns, then we only need to save the two columns of every record to the internal data source. That will reduce the view state size greatly.

 

You might have a server control like the DataGrid control (or other template based server control). That will pose an additional obstacle that you may run into is that you want to save all the information that is presented in an object. This problem can be easily overcome by using Reflection. I will leave it at that point because it goes way out of scope of this article… in fact, you may have a look at the following articles:

 

Understanding Reflection – Part One

Understanding Reflection – Part Two

 

The last article is not needed for data binding to an entire collection; however, you will get a deeper understanding of .NET from this article. Just a few tips though:

 

  1. Look at listing properties in that object that you can read.
  2. Do not consider looking at objects and properties that their access level is not “public”. Your control will run super slow if you do so.

View Entire Article

User Comments

Title: Great Article!   
Name: NiQuil
Date: 2010-01-18 9:53:29 AM
Comment:
This helped me a lot! I am totally new to programming and this has been a great tutorial for me.
I am creating my own control library with databound controls, with XHTML output and taking into account the 125 (very) strict rules for the Dutch Government Guidelines for the web.

I noticed this is a Visual studio 2003 (?) project since I had to convert it to my 2008.

Isthis still "the way to go" on this subject, or have new developments, maybe on Microsoft's part, on this subject seen the light of day?

Kind regards,

NiQuil
Title: Why Doesn't It Work   
Name: Marc
Date: 2009-06-04 3:09:50 PM
Comment:
My TitleDescriptionItems never display. CreateChildControlsnever gets hit. Why?
Title: DataSourceID   
Name: Andrew
Date: 2008-06-20 11:11:26 AM
Comment:
Great article, however I was wondering how would one implement the DataSourceID and DataMember properties?

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-02-24 12:35:24 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search