Do you keep your "DataBinder.Eval" clean?
  v1.0 Posted at 2/09/2016 4:13 AM by Tiago Araujo

Remember ASP code, you had lots of inline processing. Using DataBinder.Eval encourages the same tendencies. DataBinder.Eval is OK, so is formatting a number to a currency. But not formatting based on business rules. The general rule is, any code between <%# and DataBinder.Eval is bad and should be moved into protected method on the form.

Here is a good and a bad way to binding fields in ASP.NET in a datagrid.

Putting all the field binding code AND the business rule in the


  • Bad: Business logic is in the presentation layer (.aspx file)
  • Bad: No intellisense
  • Bad: Compile errors are not picked up 


<asp:Label id="tumorSizeLabel" runat="server" Text='<%# iif( Container.DataItem.Row.IsNull("TumorSize"), "N/A",DataBinder.Eval(Container, "DataItem.TumorSize", "0.00")) %>'/>

Bad code​​

​​Putting the code on the ItemDataBound Event.

  • Good: Business logic is in the code behind (.vb or .cs file)
  • Good: intellisense
  • Bad: Code Bloat
  • Bad: Have to use server control for all controls (viewstate bloat)

In server page: 

​​<asp:Label id="tumorSizeLabel" runat="server" /> 

In code behind:

Private Sub patientDataGrid_ItemDataBound( ByVal sender As Object, ByVal e As DataGridItemEventArgs)_
Handles patientDataGrid.ItemDataBound
If( e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim tumorSizeLabel As Label = e.Item.FindControl("tumorSizeLabel")
Dim rowView As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim row As PatientDataSet.PatientRow = CType(rowView.Row, PatientDataSet.PatientRow)
If row.IsTumorSizeNull() Then
tumorSizeLabel.Text = "N/A"
tumorSizeLabel.Text = row.TumorSize.ToString("0.00")
End If
End If
End Sub

Good code​​

​​We have a program called SSW Code Auditor to check for this rule.​​​

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address: