C# – Right way to implement data binding in ASP.NET

asp.netc

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

DataTable

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.

Best Answer

This article works up an example databound control with templating:

Building DataBound Templated Custom ASP.NET Server Controls