0

我有一个绑定到 SQL 结果的 ASP.NET 控件:

<asp:GridView ID="EmployeeSearchResults" runat="server" AutoGenerateColumns="False" >
  <Columns>
    <asp:TemplateField HeaderText="Status" SortExpression="Status">
      <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# (EmployeeSearchStatus(Eval("SeparationDate"),Eval("PositionTitle"),Eval("EffectiveDate"))) %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>                                                
  </Columns>
</asp:GridView>

我的 EmployeeSearchStatus 函数非常基础,测试传入的值是否为 NULL,并创建一个字符串来显示:

Public Function EmployeeSearchStatus(ByVal SeparationDate As Object, ByVal PositionTitle As Object, ByVal EffectiveDate As Object) As String
  Dim ReturnString As String = ""
    If IsDBNull(SeparationDate) Then
      ReturnString = "Currently Employed as "
    Else
      ReturnString = "Last Employed as "
    End If
    ReturnString += PositionTitle
    If IsDBNull(SeparationDate) Then
        ReturnString += " (effective " + EffectiveDate + ")."
    Else
        ReturnString += " (separated on " + SeparationDate + ")."
    End If
    Return ReturnString
End Function

这是处理从 SQL 返回到 ASP.NET 控件的 NULL 值的正确方法吗?有没有更好的技术?

谢谢,

罗素

4

1 回答 1

0

您的代码看起来不错,但如果您愿意,可以稍微简化一下:

Public Function EmployeeSearchStatus(ByVal SeparationDate As Object, ByVal PositionTitle As Object, ByVal EffectiveDate As Object) As String
    If IsDBNull(SeparationDate) Then
        Return "Currently Employed as " + PositionTitle + " (effective " + EffectiveDate + ")."
    Else
        Return "Last Employed as " + PositionTitle + " (separated on " + SeparationDate + ")."
    End If
End Function

但是,该代码确实假设PositionTitle并且EffectiveDate永远不会是null. 如果这在数据库中没有强制执行,您可以在代码中添加一些检查来处理这些情况。

于 2012-10-16T14:57:42.983 回答