The right way to implement data binding in ASP.NET

What is the correct way to implement the DataBind () method for a control that has a repeater inside it?

These are the requirements for this control (but you can offer your own if you think that they are missing something or are overhead)

  • The control must accept a collection or an enumeration (list of objects, anonymous objects, dictionaries, or data tables).
  • The DataSource must be completely separate from the control (using the Data * Field properties to specify properties or associated keys, such as DataValueField and DataTextField in DropDownList)
  • The control should easily navigate the ViewState. If possible, the ViewState should not be used at all, or its use should be as low as possible (store some identifier or something like this)
  • The control must handle any type (converting it using ToString ())
  • Inside ItemDataBound can use e.DataItem should be available if possible

I want my control to be initialized as follows:

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

And the data item may 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 objects

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

, , , . , , , .

+3
4

, , . :

  • , , , . , .

  • , , . - , , , "" , .

  • , , . , , . .

  • , DataTables, , , . , , .

$.02, , , (/consiseiveness/understandability/...), .

0

, , :

ASP.NET. , / . , , Domain/Business Object. ( , ..)

0

Source: https://habr.com/ru/post/1706148/


All Articles