Do you keep your "DataBinder.Eval" clean?

Last updated by Brook Jeynes [SSW] 10 months ago.See history

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 control:

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

    Text='<%# iif( Container.DataItem.Row.IsNull("TumorSize"), "N/A",DataBinder.Eval(Container, "DataItem.TumorSize", "0.00")) %>'

Figure: 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

Figure: Good code

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

We open source. Powered by GitHub