7

我想将所有 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 显示空引用错误。

你能告诉我我哪里错了吗?

4

6 回答 6

4

尝试使用会话变量。您可以将 GridView 设置为 Session 变量,然后只要同一会话仍然处于活动状态,就可以稍后检索该变量。

您可以使用以下代码在第一页上设置会话变量:

Session["gvDoctorList"] = gvDoctorList;

然后从第二页上的变量中检索:

GridView gvDoctorList = (GridView)Session["gvDoctorList"];

有关会话的更多信息,请参阅MSDN 会话状态概述

于 2013-10-09T07:59:01.913 回答
4

正如我也看到了你之前的问题,所以我可以建议你一件事,而不是让你的 gridview 保持在会话中(这很昂贵)你可以使用RowCommand事件,在button这里我认为你不需要复选框或chk_CheckedChanged事件,你可以传递PatientID到您的下一页,您可以编写查询以将选定的行数据插入到新表中。

 <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" CommandArgument='<%# 
       Eval("PatientId") %>' CommandName = "Select" />
      </ItemTemplate>
    </asp:TemplateField>



 protected void gvDoctorList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "select")
            {
                int pID = Convert.ToInt32(e.CommandArgument);
                // either put ID in session and check 
                Session["PatientID"] = Convert.ToString(pID);
                Server.Transfer("Patientstaticformatrix.aspx");
            }
        }

在 page_Load 事件

 protected void Page_Load(object sender, EventArgs e)
    {
         string pID = Convert.ToString(Session["PatientID"]);
            if(!string.IsNullOrEmpty(pID))
            {
              int patientID = Convert.ToInt32(pID);
             //Call Stored procedure which will insert this record with this ID
             // to another table
            }    

    }
于 2013-10-09T08:49:44.170 回答
3

我决定根据 Ahmed 的正确评论添加第二个答案,由于内存问题,会话变量确实不应该保存 gridview 的数据量。

对于我假设您正在做的事情,以下内容应该相应地起作用:

本质上,当您选择要转到下一页的行时,您是在尝试将该行的数据检索到新页面上。这个假设正确吗?如果是这样,那么您有许多选项可供您使用。

再次,您可以使用会话变量来存储在第一页上提取的行的数据:

protected void btnformatric_Click(object sender, EventArgs e) {
    if (gvDoctorList.SelectedRow != null) {

        GridViewRow selectedRow = gvDoctorList.SelectedRow;

        Session["PatientId"] = selectedRow.Cells[0].Text;
        Session["firstname"] = selectedRow.Cells[1].Text;
        Session["lastname"] = selectedRow.Cells[2].Text;

        Server.Transfer("Patientstaticformatrix.aspx");
    } else {
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please select a row.')", true);
    }
}

基本上,您在第一页上,您从行中获取数据。然后将此数据存储在会话变量中,您可以使用以下命令从下一页查找数据:

protected void Page_Load(object sender, EventArgs e) {
    if (this.Page.PreviousPage != null) {
        //Retrieve values from Session Variables
        Response.Write("PatientId: " + Session["PatientId"].ToString() + "<br />");
        Response.Write("firstname: " + Session["firstname"].ToString() + "<br />");
        Response.Write("lastname: " + Session["lastname"].ToString() + "<br />");
    }
}

您还可以选择使用查询字符串来传递数据。Server.Transfer("Patientstaticformatrix.aspx");虽然对于这种方法,我相信你将不得不改变Response.Redirect("Patientstaticformatrix.aspx");

下面是一个使用查询字符串的例子:

protected void btnformatric_Click(object sender, EventArgs e) {
    if (gvDoctorList.SelectedRow != null) {
        GridViewRow selectedRow = gvDoctorList.SelectedRow;
        //Create URL with Query strings to redirect to new page
        Response.Redirect("Patientstaticformatrix.aspx?parentid=" + selectedRow.Cells[0].Text + "&firstname=" + selectedRow.Cells[1].Text + "&lastname=" + selectedRow.Cells[2].Text);
    } else {
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please select a row.')", true);
    }
}

Request.QueryString并从第二页上的对象中检索值。

protected void Page_Load(object sender, EventArgs e) {
    if (this.Page.PreviousPage != null) {
        //Retrieve values from Query Strings
        Response.Write("PatientId: " + Request.QueryString["parentid"].ToString() + "<br />");
        Response.Write("firstname: " + Request.QueryString["firstname"].ToString() + "<br />");
        Response.Write("lastname: " + Request.QueryString["lastname"].ToString() + "<br />");
    }
}

这两种解决方案都应该满足您的要求,但是它们都略有不同。该Session Variable解决方案可能是首选方法,因为它将阻止用户查看所有传递的数据(如果您需要传递机密信息),因为任何可以看到 URL 的人都可以使用查询字符串值。

有关会话变量和查询字符串的更多信息,请参阅以下资源:

ASP.NET 会话状态概述

Request.QueryString 集合

于 2013-10-09T09:22:54.210 回答
0

@Nunners 的答案是自己的,但也可以尝试以下方式:

在另一个页面的页面加载事件获取网格上,例如:

GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");

所有技术如下:

http://www.aspsnippets.com/Articles/Pass-Selected-Row-of-ASPNet-GridView-control-to-another-Page.aspx

参考上面的文档。

于 2013-10-09T08:05:23.350 回答
0

真正的答案是您应该在另一个页面上创建相同的网格视图。这就是 99% 的 ASP.NET 站点的工作方式,因为该页面在某些时候会写入/更新/删除数据。或者只是使用相同的页面 - 为什么重定向以显示相同的数据?

于 2013-10-09T08:53:08.247 回答
0

我找到了一些解决方案:

在网格数据绑定后的 Source aspx 中:

Session["gridtoexcel"] =  yourgrid;

在目标 aspx

 var grid = ((GridView)Session["gridtoexcel"]);

            gridToExcel.Columns.Clear();
            foreach (DataControlField col in grid.Columns)
                  gridToExcel.Columns.Add(col);
            
            gridToExcel.DataSource = grid.DataSource;
            gridToExcel.DataBind();

这样我可以将精确的网格“克隆”到另一个页面。如果您需要一些 css 样式,请不要忘记将它们添加到目标页面

PS:gridToExcel 是您的目标网格

于 2020-07-21T15:40:29.093 回答