Declarative vs. Programmatic binding with the ASP .NET DataGrid

A co-worker and I were having an argument this morning about the best way to databind to an ASP .NET datagrid control. The argument centered around whether to use the declarative server-tag syntax in the aspx file (e.g., <%# Container.DataItem("Name") %> ) or doing it programmatically in the code-behind using the ItemDataBound() event.

I personally do not like the server-tag approach. Given that I use the code-behind to manage databinding in most other instances, and given that I often need a finer degree of presentation control than server tags give me, I prefer to write my binding code using the event model.  I realize that this approach adds a bit of complexity on the initial design side, but I find that such code is easier to modify and extend in the long run than server tags. I also like knowing that all of my binding code is in one place, and that there is a consistent approach to databinding throughout the system, making maintenance easier. Extensibility is also a concern because if the needs of the binding exceed what is normally accessible via server-tags, the binding logic has to be redeveloped from scratch in the code-behind. Finally, the server-tags approach smells of classic ASP to me; and while it's a purely emotional response, I want to stay as far away from intermingling presentation and programming logic as possible.

My co-worker recognizes that there are many instances where programmatic databinding is necessary, but argues that it's often easier to just use declarative server-tags.  His argument is that the speed-of-development on the front-end is worth the possible slower maintainability and extensibility in the future.

I realize that I'm not giving my co-worker equal space here, but in truth, that's about all he said. I'm interested if anyone else has an opinion on this subject, and what other issues may be present that we're not considering.

Thoughts?

<%# Container.DataItem("AttorneyName")%>

Comments

# re: Declarative vs. Programmatic binding with the ASP .NET DataGrid

Wednesday, May 18, 2005 2:53 PM by Karl

I use OnItemDataBound when I have to and not when I don't. I certainly run into both cases frequently. If, in the future, new needs require more advanced formatting/processing, it's typically trivial to move all/parts to the OnItemDataBound event.

"Always implement things when you actually need them, never when you just foresee that you need them" sums up my view of this one...

# re: Declarative vs. Programmatic binding with the ASP .NET DataGrid

Wednesday, May 18, 2005 3:44 PM by Benjimawoo

I'm afraid I'm with your co-worker.

I generally use server tags in the HTML, particularly if I've got some weird and wonderful formatting going on in my table. I find it easier to picture what's going on with all the layout and content bunched together.

Although I also use <%# GetMyWeirdlyFormattedString(Container.DataItem("Name")) %>

if it's very weird.

Oh well. Guess we're all different

# re: Declarative vs. Programmatic binding with the ASP .NET DataGrid

Wednesday, May 18, 2005 3:58 PM by Anatoly

We build DataGrid columns in code-behind.
1)Declaration of DataGridColumn on page level makes easy to reference it on every DataGrid event(by Column index)
2)We can change the way Grid looks and behaves by changing code in one place(Lets say we have corporative Date format and you want to change it)
3)In future we will be able to hold Grid columns information in configuration file/database

# re: Declarative vs. Programmatic binding with the ASP .NET DataGrid

Wednesday, May 18, 2005 4:38 PM by Wilco Bauwer

I too am with your co-worker. Doing it The Declarative Way makes it, IMO, easier to maintain (a designer can add/remove things if he/she like without having to worry about breaking dependent code). Doing everything on ItemDataBound also means that you somehow have to find the right controls (assuming you don't build the whole control hierarchy for each item there). This _may_ be problematic in certain situations when dealing with container controls (read: controls implementing INamingContainer).

Sometimes you have to do more than simply output data in the data object. In that case I would add helper methods in the code behind and call that, like Benjimawoo suggested. That way I think you generally have the best of both worlds.

# re: Declarative vs. Programmatic binding with the ASP .NET DataGrid

Thursday, May 19, 2005 4:39 AM by Jeff Gonzalez

Or you could stay away from the DataGrid altogether. We have used repeaters extensively across our applications. We wrote a custom paging control as well as built in sorting. The render time on a repeater is much shorter than a datagrid and you can control the html without having to override the lifecycle methods of the control.

I figure someone will say something about tediousness of writing html with the repeater, but we code generate most everything for list pages, and even if we have to customize something at least 60% or more of the work has already been done.

# re: Declarative vs. Programmatic binding with the ASP .NET DataGrid

Thursday, May 19, 2005 6:52 PM by Javier Luna

I believe that any DataLayer must be a simple code block, that they allow operations against DB.

That code block would not have to know on the Business Entities. Single to specialize it is to execute the operations (Store Procedures and SQL Sentences) against the engine DB (SQL, Oracle, DB2, etc.), with which this setting.

Finally, I invite to you to download the DataLayer.Primitives Public Version.

This is very cool Data Layer :)

DataLayer.Primitives - Readme!
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=1389

Cheers,

Javier Luna
http://guydotnetxmlwebservices.blogspot.com/

# re: Declarative vs. Programmatic binding with the ASP .NET DataGrid

Monday, May 23, 2005 10:12 PM by Aaron

I rarely use DataGrids - I just find that they're too restrictive and I have too much overhead. I'm a big fan of simply using repeaters to code up lists that look/feel exactly the way I want. Everytime I start with a DataGrid I end up replacing it as the functionality of the list grows. That being said, I prefer the declarative method. When I'm already using an <asp:... > control I try to keep as much of the presentation with the control rather than pushing it into the code behind. It usually ends up cleaner and easier to read.

# re: Declarative vs. Programmatic binding with the ASP .NET DataGrid

Tuesday, May 31, 2005 11:22 AM by Michael Stone

My 2 cents:

I go with the repeater/code behind approach for the simple reason that a code change will break at compile time.

This:
<%# Container.DataItem("AttorneyName")%>
will not break until you visit the page if you've changed the object that is populating the control.

This:
private void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
// Get the object for this row.
AnObject o = e.Item.DataItem as AnObject;
if (o!= null)
{
// Set the fields.
Label name = e.Item.FindControl("labelName") as Label;
if (name != null)
{
name.Text = o.Name;
}
}
}

Will break at compile time if AnObject has changed.
Of course, this means you have strongly typed objects instead of SQL queries but the only place a SQL query should exist is in the DAL.

# re: Declarative vs. Programmatic binding with the ASP .NET DataGrid

Wednesday, September 26, 2007 1:36 AM by tlsterling

There is a ViewState symptom to consider in the realm of programmatic versus declarative server control initiation debate; at least within the 2.0 framework.  

If you rely on a server control’s view state to retain its data across postbacks, so that you’re not required to continuously re-load the control every time the page refreshes, asp will store your entire data set for that control in view state (that’s about 136 lines x 1024 characters worth of view state for a single DropDownList control populated with approximately 5000 records).  All changes made to server controls via the code-behind file are lumped in with asp’s interpreted "user-initiated control changes" and so are preserved within view state rather than stored server-side.

Alternatively, declaratively binding a server control to a DataSource object, such as a SQLDataSource or an ObjectDataSource, will store the control’s associated dataset server-side eliminating the size-able view state that will travel to/from server and users' browsers for each page request.            

Similarly, any property setting changes made to controls in a programmatic manner, such as the control's styles, maxLength, databindings, etc will ALSO be recorded and maintained within the page's view state.

Is this really an issue when you're talking about rich server controls such as the GridView which already occupy more than a substantial amount of ViewState?  That all depends on your unique situation, but it's food for thought.    

# ~Hikaru {Aka}Yuuki~ &raquo; Blog Archive &raquo; Declarative vs Programmatic binding with ASP. NET

Pingback from  ~Hikaru {Aka}Yuuki~  &raquo; Blog Archive   &raquo; Declarative vs Programmatic binding with ASP. NET

# Well&#8230;I do DEE-CLAY-AIRE&#8230; &laquo; Bloggerriffic!!

Wednesday, December 12, 2007 6:25 PM by Well…I do DEE-CLAY-AIRE… « Bloggerriffic!!

Pingback from  Well&#8230;I do DEE-CLAY-AIRE&#8230; &laquo; Bloggerriffic!!

Leave a Comment

(required) 
(required) 
(optional)
(required)