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

Convert Energy – You cannot destroy it

In Physical Science (if you did it to grade nine), you would have learnt that you cannot lose energy – you can only convert the energy to another form. For example, when a fire burns, it gives off heat. That heat is then transferred into a light energy.

 

Similarly, when you develop a control that data source that is given should be converted to an offline version. The only reason for the data source to be converted to an offline version is so that during post backs, that data can be used by your control to re-populate itself.

 

ADO.NET fits perfectly into this situation because, in summary, ADO.NET provides an offline abstract view of the data. Specifically, the DataTable will be best to be utilized because it is easily serialized into an XML mark-up (text).

 

So, what I have told you so far, you will need to do the following:

 

  1. Get an object in the data source that implements the ICollection interface.
  2. Convert the data source into a DataTable so that it may be used for every post back.

However, I have to look at my review of what must be done and pause at point one and look at point two… then the thought that the page developer might want to feed us a data reader. “Oh no,” one might exclaim.

 

That does not really affect us because we can skip straight into point two. We can cleverly convert a data reader into a DataTable within a flash. The code sample listed below takes the data reader and finds a DataTable make-up (that is, a schema table):

 

public object DataSource {
   get { return ViewState["DataSource"]; }
   set {
      if (DataSource is DataTable)
         ViewState["DataSource"] = value;
      else if (value is IDataReader) {
         IDataReader reader = (IDataReader)value;
         // gets the column based table (schema)
         DataTable table = reader.GetSchemaTable();
 
         while (reader.Read()) {
            DataRow row = table.NewRow();
            table.Rows.Add(row);
 
            foreach (DataColumn column in table.Columns)
               row[column.ColumnName] = reader[column.ColumnName];
         }

         DataSource = table;
      }
   }
}

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-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 11:37:40 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search