2013-01-01 32 views
0

我得到一個空的異常拋出。你能幫我理解爲什麼嗎?代碼後面的空例外

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Collections; 
using System.Data; 
using System.Data.SqlClient; 


namespace ProjectEta 
{ 
    public partial class File_Viewer : System.Web.UI.Page 
    { 
     public string CurDate = DateTime.Today.ToString("dd/MM/yyyy"); 

     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) 
     { 
      if (e.Item.ItemType == ListViewItemType.DataItem) 
      { 

       TextBox OpenDateTextBox = (TextBox)e.Item.FindControl("OpenDateTextBox"); 

       OpenDateTextBox.Text = "12/12/2012"; 

       DataRowView rowView = e.Item.DataItem as DataRowView; 
       string myCurDate = rowView["OpenDate"].ToString(); 
      } 

     } 

    } 
} 

這是aspx。

<InsertItemTemplate> 
      <tr style="font-size: smaller; text-align: center;"> 
       <td> 
        <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" /> 
        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" /> 
       </td> 
       <td>&nbsp</td> 
       <td> 
        <asp:DropDownList ID="ProcessorIdDrop" runat="server" DataSourceID="UserNames" DataTextField="Name" Text='<%# Bind("ProcessorId") %>' ></asp:DropDownList> 
       </td> 
       <td> 
        <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="UserNames" DataTextField="Name" Text='<%# Bind("UnderwriterId") %>' ></asp:DropDownList> 
       </td> 
       <td> 
        <asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="FileStatusTypes" DataTextField="FileStat" Text='<%# Bind("Status") %>' ></asp:DropDownList> 

       </td> 
       <td> 
        <asp:TextBox ID="OpenDateTextBox" runat="server" Text='<%# Bind("OpenDate") %>' /> 
       </td> 
       <td> 
        <asp:Label ID="CloseDateTextBox" runat="server" Text='<%# Eval("CloseDate") %>' /> 
       </td> 
       <td> 
        <asp:TextBox ID="BorrowerIdTextBox" runat="server" Text='<%# Bind("BorrowerId") %>' /> 
       </td> 
       <td> 
       <asp:DropDownList ID="Lender_LenderIdDrop" runat="server" DataSourceID="LenderNames" DataTextField="Name" DataValueField="Name" Text='<%# Bind("Lender_LenderId") %>'></asp:DropDownList> 
       </td> 
       <td> 
        <asp:TextBox ID="Client_ClientIdTextBox" runat="server" Text='<%# Bind("Client_ClientId") %>' /> 
       </td> 
      </tr> 
     </InsertItemTemplate> 

我跟着這個職位,how to set label text inside listview from code behind的建議,但我得到一個「對象引用不設置到對象的實例」。我以爲OpenDateTextBox是aspx中文本框的ID,所以我不會遇到這個問題。我得到這是一個noob問題,但幫助將不勝感激。

+0

您正在使用的變量之一包含'null',可能是因爲其初始化程序返回'null'。讓我們知道哪條線導致這一點。 –

+0

你可以粘貼堆棧跟蹤嗎? –

+0

它也有可能'e.Item.FindControl(「OpenDateTextBox」)'返回null,因爲它無法找到它。 –

回答

0

您得到NullReferenceException,因爲TextBox不在ItemTemplate之內,而是在InsertItemTemplate之內。也

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) 
{ 
    // note the ListViewItemType.InsertItem !!! 
    if (e.Item.ItemType == ListViewItemType.InsertItem) 
    { 

     TextBox OpenDateTextBox = (TextBox)e.Item.FindControl("OpenDateTextBox"); 
     OpenDateTextBox.Text = "12/12/2012"; 
    } 
} 

請注意,我刪除這些行:所以,你必須檢查ListViewItemType.InsertItem而不是DataItem

DataRowView rowView = e.Item.DataItem as DataRowView; 
string myCurDate = rowView["OpenDate"].ToString(); 

由於在InsertItemTemplate的(當然)沒有DataItem

+0

謝謝,它修復了異常並讓頁面呈現。但是,當我做一個插入我沒有得到2012年12月12日在單元格。我會進一步深入研究。非常感謝,並感謝耐心與noobs。 – stamm528

0

真正的問題似乎是事件的選擇。我從ItemDataBound更改爲ItemCreated,它工作得很好。感謝那些提供了指導。