我想将所有 gridview 值传递到另一个页面我在 PatientDetails.aspx 页面中有一个 gridview 和一个按钮,如下所示
<asp:GridView ID="gvDoctorList" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
AllowPaging="True" AllowSorting="True" AutoGenerateEditButton="true" AutoGenerateSelectButton="true"
AutoGenerateDeleteButton="true" OnSelectedIndexChanged="gvDoctorList_SelectedIndexChanged" OnRowCommand="gvDoctorList_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chk" OnCheckedChanged="chk_CheckedChanged" AutoPostBack="true" />
<asp:Label runat="server" ID="lblPID" Visible="false" Text='<%# Eval("PatientId") %>'></asp:Label>
<asp:Button ID="btnSelect" runat="server" Text="Select" CommandName = "Select" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PatientId" HeaderText="PatientId" SortExpression="PatientId" />
<asp:BoundField DataField="firstname" HeaderText="firstname" SortExpression="firstname" />
<asp:BoundField DataField="lastname" HeaderText="lastname" SortExpression="lastname" />
<asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDatabaseConnectionString %>"
SelectCommand="SELECT [PatientId],[firstname], [lastname], [sex] FROM [PatientDetails]"></asp:SqlDataSource>
<asp:Button ID="btnformatric" runat="server" Text="formatric3d" OnClick="btnformatric_Click" OnCommand="btnformatric_Command" />
PatientDetails.aspx 的代码隐藏如下
protected void btnformatric_Click(object sender, EventArgs e)
{
if (gvDoctorList.SelectedRow != null)
{
Server.Transfer("Patientstaticformatrix.aspx");
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please select a row.')", true);
}
}
现在在第二页名称 Patientstaticformatrix.aspx 后面的代码如下
protected void Page_Load(object sender, EventArgs e)
{
if (this.Page.PreviousPage != null)
{
GridView gvDoctorList = (GridView)this.Page.PreviousPage.FindControl("gvDoctorList");
GridViewRow selectedRow = gvDoctorList.SelectedRow;
Response.Write("PatientId: " + selectedRow.Cells[0].Text + "<br />");
Response.Write("firstname: " + selectedRow.Cells[1].Text + "<br />");
Response.Write("lastname: " + selectedRow.Cells[2].Text + "<br />");
}
}
我已经在第二页调试了代码...... gvDoctorList 的值为空,并且 selectedRow 显示空引用错误。
你能告诉我我哪里错了吗?