【发布时间】: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];
【问题讨论】: