Tutorial :Right way to implement data binding in ASP.NET


What is the right way to implement DataBind() method for control which have a repeater inside of it?

These are requirements for this control (but you can offer yours if think these are missing of something or are an overhead)

  • Control should accept a collection or an enumerable (list of objects, anonymous objects, dictionaries or data table)
  • DataSource should be completely should be completely decoupled from the control (using Data*Field properties to specify properties or keys mapped; like DataValueField and DataTextField in DropDownList)
  • The control should go easy on ViewState. If possible ViewState shouldn't be used at all, or it's usage should be as low as possible (store some ID or something like this)
  • The control should handle any type (converting it using ToString())
  • Inside of ItemDataBound be able to use e.DataItem should be accessible if possible

I want my control to be initialized like so:

var control = new Control();  control.DataDateField = "Date";  control.DataNameField = "FullName";  control.DataTextField = "Comment";  control.DataSource = data;  control.DataBind();  

And data item can be one of the following

List of dictionaries (or table rows)

var data = new List<Dictionary<string, string>>  {      new Dictionary<string, string>          {{"Date", "2009-03-15"}, {"FullName", "John Walker"}, {"Comment", "comment1"}},      new Dictionary<string, string>          {{"Date", "2009-03-12"}, {"FullName", "Chuck Norris"}, {"Comment", "comment2"}},      new Dictionary<string, string>          {{"Date", "2009-03-13"}, {"FullName", "Sergej Andrejev"}, {"Comment", "comment3"}}  };  

List of anonymous objects

var data = new List<object>  {      new {Date = "2009-03-15", FullName = "John Walker", Comment = "comment1"},      new {Date = "2009-03-12", FullName = "Chuck Norris", Comment = "comment2"},      new {Date = "2009-03-13", FullName = "Sergej Andrejev", Comment = "comment3"},  };  

List of ojects

public class SampleClass  {      public object Date { get; set; }      public object FullName { get; set; }      public object Comment { get; set; }        public SampleClass(string date, string fullName, string comment)      {          Date = date;          FullName = fullName;          Comment = comment;      }  };    var data = new List<SampleClass>  {      new SampleClass("2009-03-15", "John Walker", "comment1"),      new SampleClass("2009-03-12", "Chuck Norris", "comment2"),      new SampleClass("2009-03-13", "Sergej Andrejev", "comment3"),  };  


var data = new DataTable();  data.Columns.Add(new DataColumn { DataType = typeof(DateTime), ColumnName = "Date" });  data.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "FullName" });  data.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "Comment" });    data.Rows.Add(new object[] { DateTime.Parse("2009-03-15"), "John Walker", "comment1" });  data.Rows.Add(new object[] { DateTime.Parse("2009-03-12"), "Chuck Norris", "comment2" });  data.Rows.Add(new object[] { DateTime.Parse("2009-03-13"), "Sergej Andrejev", "comment3" });  

Basically what I want is universal code for data binding so I wouldn't waste time creating it from scratch every time I create new control. I would appreciate any references to good practices, official guides and of course your personal experience.


This article works up an example databound control with templating:

Building DataBound Templated Custom ASP.NET Server Controls


Here is what I think you are looking for...


It is a base class that your control can inherit from...


Each way probably has it's pros and cons. Here's what I see:

  1. Dictionaries are nice because they're very common objects, everybody should know how to use and manipulate them. The syntax can get ugly though.

  2. Anonymous objects have great looking, clean syntax, and they're also great at quickly implementing a one-off solution. I tried using them for something similar, but quickly ran into some limitations, so if you need to do real 'work' with them, you might end up having to scrap them.

  3. A list of objects is similar to a dictionary, but lends itself to easier understanding of what you're trying to accomplish. You have greater readability and easier understandability, but it's another object in your library that's only going to get used once. It might be too much typing for what a simple problem.

  4. I think I've used DataTables like you did above one time because I didn't know what I was doing. If it's between that and a Dictionary, use a Dictionary since they're more common and generic objects.

Just my $.02, but it really comes down to what's most important to you for the problem you're solving(readability/consiseiveness/understandability/...) and if there are already and conventions in your current codebase.



I know this does not fully answer your question, but as supplemental information:

You may also want to look into ASP.NET Dynamic Data. It uses the data type of the property being bound to determine the kind of control/user control to display. It also adds additional functionality based on what that data represents in the context of the entire Domain/Business Object Model. (if it is an ID Field, foreign key, etc.)

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »