【问题标题】:Object reference not set to an instance of an object while passing data from gridview从 gridview 传递数据时,对象引用未设置为对象的实例
【发布时间】:2014-04-04 17:43:37
【问题描述】:

我正在将值从网格视图传递到下一页。 gridview 连接到数据库。gridview 显示我想要的数据,但是当我尝试将 gridview 的行数据传递到下一页时,它会引发错误“对象引用未设置为对象的实例”。

gridview如下:

      <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
      RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
   runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowPaging="True">
   <AlternatingRowStyle BackColor="White" ForeColor="#000000"></AlternatingRowStyle>
    <Columns>
    <asp:BoundField DataField="TaskId" HeaderText="TaskId" ItemStyle-Width="30" InsertVisible="False" ReadOnly="True" SortExpression="TaskId" />
    <asp:BoundField DataField="Title" HeaderText="Title" ItemStyle-Width="150" SortExpression="Title" />
    <asp:BoundField DataField="Body" HeaderText="Body" SortExpression="Body" />
    <asp:BoundField DataField="Reward" HeaderText="Reward" SortExpression="Reward" />
    <asp:BoundField DataField="TimeAllotted" HeaderText="TimeAllotted" SortExpression="TimeAllotted" />
    <asp:BoundField DataField="PosterName" HeaderText="PosterName" SortExpression="PosterName" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="lnkDetails" runat="server" Text="Send Details" PostBackUrl='<%# "~/test/Tasks.aspx?RowIndex=" + Container.DataItemIndex %>'></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
 <HeaderStyle BackColor="#3AC0F2" ForeColor="White"></HeaderStyle>

 <RowStyle BackColor="#A1DCF2"></RowStyle>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ABCD %>" SelectCommand="SELECT * FROM [Task]"></asp:SqlDataSource>

'Tasks.aspx.cs'的代码如下:

 protected void Page_Load(object sender, EventArgs e)
{
    if (this.Page.PreviousPage != null)
    {
        int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
        GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
        GridViewRow row = GridView1.Rows[rowIndex];
        lblTaskId.Text = row.Cells[0].Text;
        lblTitle.Text = row.Cells[1].Text;
        lblBody.Text = row.Cells[2].Text;
        lblReward.Text = row.Cells[3].Text;
        lblTimeAllotted.Text = row.Cells[4].Text;
        lblPosterName.Text = row.Cells[5].Text;


    }
}

我在下面一行收到一条错误消息:

   GridViewRow row = GridView1.Rows[rowIndex];

【问题讨论】:

    标签: c# asp.net .net gridview


    【解决方案1】:

    看起来FindControl 方法实际上并没有找到控件,使您的GridView1 变量为空。确保控件位于页面内部并且名称正确。

    编辑:

    如果您使用母版页,FindControl 确实存在一些问题。 Rick Strahls 在his blog 中解释它:

    *The problem is that when you use MasterPages the page hierarchy drastically changes.
    Where a simple this.FindControl() used to give you a control instance you now have to
    drill into the container hierarchy pretty deeply just to get to the content container.*
    

    他还举了一个例子来说明如何让它发挥作用。基本上,您必须从母版页中找到您的内容控件(使用page.Master.FindControl)。然后从那个点你应该能够到达你的网格控件。

    在您的场景中,因为您想要上一页的控件:

    var GridView1 = page.PreviousPage.Master.FindControl("ContentContainer").FindControl("GridView1") as GridView;
    

    【讨论】:

    • 我该怎么办?
    • 所以我不应该使用 previouspage 命令?但是当我使用上面的代码时,它说“页面”不存在于当前上下文中
    • @Mash 抱歉,我忘了输入PreviousPage 访问权限。再次查看我编辑的答案。当然请记住,您的容器名称可能会有所不同。
    【解决方案2】:

    在我看来,当“this.Page.PreviousPage != null”为真时,您的 gridview 没有任何绑定数据。也就是说,除非您在其他初始化处理程序中绑定数据。

    因为没有数据,所以没有 Rows,这意味着 GridView1.Rows[rowIndex] 将为空。

    【讨论】:

    • 我明白这一点。一个有趣的事情是,当我使用相同的设置而没有任何母版页时,它可以工作。那是什么?
    • 问题与数据无关。如果是这样,则该行不会抛出NullReferenceException
    • 啊,您最初的问题没有提到您使用的是母版页。这确实有道理。很高兴它对你有用。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多