2013-02-28 55 views
0

我想在FormView中建立一个地址,该地址依赖于具有联系人名称列表的GridView。如何在尝试不在FormView ASP.net中打印空字段时捕获错误?

当我点击GridView中票据名称旁边的选择按钮时,它会在FormView中显示票据地址。当我第二次点击它时,bill已经被选中,它会抛出一个NullReferenceException错误。

我有以下字段:地址行,address_line_2,address_line_3

我曾试图建立的代码,以便它不会产生空白的浪费行,如果该字段为空或包含空。

<asp:FormView ID="addressDetails" runat="server" DataSourceID="ADetails" DataKeyNames="address_id" AllowPaging="true" > 
    <ItemTemplate> 
     <% If Not String.IsNullOrEmpty(addressDetails.DataItem("address_line_1").ToString()) Then%> 
      <asp:Label ID="lblAddressLine1" runat="server" Text='<%# Bind("address_line_1") %>' /><br /> 
     <% End If%> 
     <% If Not String.IsNullOrEmpty(addressDetails.DataItem("address_line_2").ToString()) Then%> 
      <asp:Label ID="lblAddressLine2" runat="server" Text='<%# Bind("address_line_2") %>' /><br /> 
     <% End If%> 
     <% If Not String.IsNullOrEmpty(addressDetails.DataItem("address_line_3").ToString()) Then%> 
      <asp:Label ID="lblAddressLine3" runat="server" Text='<%# Bind("address_line_3") %>' /><br /> 
     <% End If%> 
    </ItemTemplate> 
</asp:FormView> 

我得到的例外是“的NullReferenceException是由用户代码未处理的”在行:

<% If Not String.IsNullOrEmpty(addressDetails.DataItem("address_line_1").ToString()) Then%> 

有谁知道为什么它工作第一次,但不是第二个?

+0

这也是我现在开发MVC而不是WebForms的部分原因。构建和调试Gridviews和Listviews等是浪费时间。 – 2013-02-28 15:49:29

回答

0

我最终重新整理了一大块页面,并删除了aspx页面中的If语句。

相关问题