Do you keep your "DataBinder.Eval" clean?
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
<asp:Labelid="tumorSizeLabel"runat="server"Text='<%# iif( Container.DataItem.Row.IsNull("TumorSize"), "N/A",DataBinder.Eval(Container, "DataItem.TumorSize", "0.00")) %>'/>
❌ Figure: 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.ItemDataBoundIf( e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) ThenDim 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() ThentumorSizeLabel.Text = "N/A"ElsetumorSizeLabel.Text = row.TumorSize.ToString("0.00")End IfEnd IfEnd Sub
✅ Figure: Figure: Good code
We have a program called SSW Code Auditor to check for this rule.