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:
我将值从gridview传递到下一页.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>
The code for 'Tasks.aspx.cs' is as below:
'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;
}
}
I get an error message on the line below:
我在下面的行中收到错误消息:
GridViewRow row = GridView1.Rows[rowIndex];
Somebody please help me on this.i have been trying to get this done for 3 days now.
有人请帮我解决这个问题。我一直试图让这件事完成3天。
2 个解决方案
#1
3
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方法实际上并没有找到控件,使得GridView1变量为null。确保控件在页面内部并且名称正确。
EDIT:
编辑:
If you are using master pages, there's indeed some issues with FindControl
. Rick Strahls explains it in his blog:
如果您使用的是母版页,那么FindControl确实存在一些问题。 Rick Strahls在他的博客中解释道:
*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.*
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.
他还有一个如何使其发挥作用的例子。基本上,您必须从母版页中找到您的内容控件(使用page.Master.FindControl)。然后,从那时起,您应该能够达到您的网格控制。
In your scenario, since you want a control from the previous page:
在您的方案中,因为您需要上一页的控件:
var GridView1 = page.PreviousPage.Master.FindControl("ContentContainer").FindControl("GridView1") as GridView;
#2
0
It looks to me like your gridview does not have any bound data when "this.Page.PreviousPage != null" is true. That is, unless you bind your data in some other initialization handler.
在“this.Page.PreviousPage!= null”为真时,我认为你的gridview没有任何绑定数据。也就是说,除非您在其他一些初始化处理程序中绑定数据。
Because there is no data, there are no Rows which means that GridView1.Rows[rowIndex] would be null.
因为没有数据,所以没有Rows,这意味着GridView1.Rows [rowIndex]将为null。
#1
3
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方法实际上并没有找到控件,使得GridView1变量为null。确保控件在页面内部并且名称正确。
EDIT:
编辑:
If you are using master pages, there's indeed some issues with FindControl
. Rick Strahls explains it in his blog:
如果您使用的是母版页,那么FindControl确实存在一些问题。 Rick Strahls在他的博客中解释道:
*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.*
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.
他还有一个如何使其发挥作用的例子。基本上,您必须从母版页中找到您的内容控件(使用page.Master.FindControl)。然后,从那时起,您应该能够达到您的网格控制。
In your scenario, since you want a control from the previous page:
在您的方案中,因为您需要上一页的控件:
var GridView1 = page.PreviousPage.Master.FindControl("ContentContainer").FindControl("GridView1") as GridView;
#2
0
It looks to me like your gridview does not have any bound data when "this.Page.PreviousPage != null" is true. That is, unless you bind your data in some other initialization handler.
在“this.Page.PreviousPage!= null”为真时,我认为你的gridview没有任何绑定数据。也就是说,除非您在其他一些初始化处理程序中绑定数据。
Because there is no data, there are no Rows which means that GridView1.Rows[rowIndex] would be null.
因为没有数据,所以没有Rows,这意味着GridView1.Rows [rowIndex]将为null。