而来自GridView控件传递数据的对象引用不设置到对象的实例 [英] Object reference not set to an instance of an object while passing data from gridview

查看:180
本文介绍了而来自GridView控件传递数据的对象引用不设置到对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递值从一个GridView到下一PAGE电泳GridView控件连接到database.The gridview的显示数据,我想,但是当我尝试的GridView的行数据传递到下一个页面,它抛出一个错误对象引用不设置到对象的实例
在GridView如下:

I am passing values from a gridview to next page.The gridview is connected to a database.The gridview shows the data as i want but when i try to pass the row data of gridview to next page it throws an error ' Object reference not set to an instance of an object' The gridview is as follows:

      <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>

在code为'Tasks.aspx.cs'是如下:

The code for 'Tasks.aspx.cs' is as below:

 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;


    }
}

我得到在下面的横线的错误消息:

I get an error message on the line below:

   GridViewRow row = GridView1.Rows[rowIndex];

有人请帮我this.i一直在试图让本作现在是3天进行。

Somebody please help me on this.i have been trying to get this done for 3 days now.

推荐答案

这似乎是的FindControl 方法实际上并没有找到控制,使您的 GridView1 变空。确保控制以及在页面内和名称是正确的。

It would seem that FindControl method isn't actually finding the control, making your GridView1 variable null. Ensure the control is well inside the page and that the name is right.

编辑:

如果您使用的是母版页,有的确一些问题,的FindControl 。里克Strahls解释了它在他的博客

If you are using master pages, there's indeed some issues with FindControl. Rick Strahls explains it in 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 )找到母版页的内容控制。然后,从的的点你应该能够达到你的网格控件。

He also has an example on how to make it work. Basically, you must find your content control from the master page (using page.Master.FindControl). Then from that point you should be able to reach to your grid control.

在您的情况,因为你要从previous页面控件:

In your scenario, since you want a control from the previous page:

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

这篇关于而来自GridView控件传递数据的对象引用不设置到对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆