Sergej Andrejev
Sergej Andrejev

Reputation: 9403

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)

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.

Upvotes: 3

Views: 871

Answers (4)

Aaron Hoffman
Aaron Hoffman

Reputation: 6962

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.)

Upvotes: 0

MStodd
MStodd

Reputation: 4746

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.

Mark

Upvotes: 0

Jason DeFontes
Jason DeFontes

Reputation: 2285

This article works up an example databound control with templating:

Building DataBound Templated Custom ASP.NET Server Controls

Upvotes: 6

bytebender
bytebender

Reputation: 7491

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

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.databoundcontrol.aspx

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

Upvotes: 2

Related Questions