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>

我遵循了这篇文章的建议,如何从后面的代码中设置列表视图中的标签文本,但我得到一个“对象引用未设置为对象的实例”。我想因为 OpenDateTextBox 是 aspx 中文本框的 ID,所以我不会遇到这个问题。我知道这是一个菜鸟问题,但我们将不胜感激。

4

2 回答 2

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();

既然没有DataItemInsertItemTemplate(当然)。

于 2013-01-01T21:54:02.843 回答
0

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

于 2013-01-03T00:34:37.167 回答