代码背后的空例外

时间:2013-01-01 21:38:45

标签: c# nullreferenceexception

我收到了一个null异常。你能帮我理解为什么吗?

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问题,但是我们将不胜感激。

2 个答案:

答案 0 :(得分:0)

您获得了NullReferenceException,因为TextBox不在ItemTemplate内,而在InsertItemTemplate内。因此,您必须检查ListViewItemType.InsertItem而不是DataItem

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";
    }
}

另请注意,我已删除这些行:

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

由于DataItem(当然)中没有InsertItemTemplate

答案 1 :(得分:0)

真正的问题似乎是事件的选择。我从ItemDataBound更改为ItemCreated,它工作得很好。感谢那些提供指导的人。