2013-02-28 27 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語句。