Transfer ASP.Net GridView from one page to another page

I want to transfer the entire gridview value to another page I have one gridview on the PatientDetails.aspx page and one button below

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

on codebehind PatientDetails.aspx below

 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); } } 

Now on the second page the name Patientstaticformatrix.aspx is the code below as shown below

 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 />"); } } 

I debugged the code on the second page .... the value for gvDoctorList is null, and also the selected Row value shows a zero output error.

Could you tell me where I am wrong?

+7
c # gridview
source share
5 answers

As I already saw your previous question, so I can offer you one thing, instead of holding your gridview in a session (which is expensive), you can use the RowCommand event, and after the button here I don’t know if you need a flag or event chk_CheckedChanged , you can pass PatientID to the next page, there you can write a query to insert the selected row data into your new table.

  <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"); } } 

On the page_Load Event

  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 } } 
+4
source share

Try using session variables. You can set the GridView to a session variable, which can then be retrieved later while the same session is still active.

You can use the following code to set the session variable on the first page:

 Session["gvDoctorList"] = gvDoctorList; 

And then, to return to the variable on your second page:

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

For more information about sessions, see MSDN Session Status Overview .

+4
source share

I decided to add a second answer based on the correct comments from Ahmed. Session variables really should not contain the amount of gridview data due to memory issues.

The following should work accordingly for what I assume you are doing:

In fact, when you select a row to go to the next page, you are trying to get the data of this row on a new page. Is this Assumption right? If so, you have several use cases.

Again, you can use session variables to store the data of the string extracted on the first page:

 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); } } 

Essentially here you are on the first page and get the data from the row. You then store this data in session variables, and you can use the following data to find data from the following page:

 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 />"); } } 

You also have a second option for using Query Strings to transfer data. Although for this method I believe you will have to change Server.Transfer("Patientstaticformatrix.aspx"); as Response.Redirect("Patientstaticformatrix.aspx");

The following is an example of using query strings:

 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); } } 

And get the values ​​from the Request.QueryString object on the second page.

 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 />"); } } 

Both of these solutions should meet your requirements, but they are both slightly different. The Session Variable solution is probably the preferred method, because it will not allow users to see all the transmitted data (if you need to transmit confidential information), where, since the values ​​of the query string will be available to anyone who can see the URL,

For more information about session variables and query strings, see the following resources:

ASP.NET Session Status Overview

Request.QueryString Collection

+1
source share
Answer to

@Nunners is own, but he can also try as follows:

on the pogeload event extraction grid of the anode page, like:

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

All equipment is given below:

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

See above documentation.

0
source share

The real answer is that you have to create the same gridview on another page. So 99% of ASP.NET sites work, because at some point this page will write / update / delete data. Or just use the same page - why redirect to show the same data?

0
source share

All Articles