Pass ASP.Net GridView from one page to another page

前端 未结 6 1576
终归单人心
终归单人心 2021-01-12 13:59

I want to pass all the gridview value into another page I have one gridview in PatientDetails.aspx page and one button as below



        
相关标签:
6条回答
  • 2021-01-12 14:37

    @Nunners answer is ownsome, but can also try with following way:

    on anothoer page's pageload event fetch grid like:

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

    All the technique is given below:

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

    Refer above doccument.

    0 讨论(0)
  • 2021-01-12 14:45

    As i have seen your previous question also, So i can suggest you one thing, rather than keeping your gridview in session(which is expensive) you can use RowCommand event, and after having button here i don't think you need checkbox or chk_CheckedChanged event, you can pass the PatientID to your next page there you can write query to insert selected row data to 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 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
                }    
    
        }
    
    0 讨论(0)
  • 2021-01-12 14:50

    Try using Session Variables. You can set the GridView into a Session variable that can then be retreived later so long as the same session is still active.

    You can use the following code to set the Session Variable on your first page :

    Session["gvDoctorList"] = gvDoctorList;
    

    And then to retreive from the variable on your second page :

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

    For more information on Sessions see the MSDN Session State Overview.

    0 讨论(0)
  • 2021-01-12 14:53

    I found some solution:

    In Source aspx after grid databind:

    Session["gridtoexcel"] =  yourgrid;
    

    In destination 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();
    
    

    this way i can 'clone' exact grid to another page. if you need some css style don't forget of add them in destination page

    PS: gridToExcel is your destination grid

    0 讨论(0)
  • 2021-01-12 14:54

    The real answer is that you should create the same gridview on another page. This is how 99% of ASP.NET sites work since that page at some point will be writing/updating/deleting data. Or just use the same page - why redirect to show the same data?

    0 讨论(0)
  • 2021-01-12 14:57

    I have decided to add a second answer based on the correct comments from Ahmed, The session variables really shouldn't hold the amount of data of the gridview due to memory issues.

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

    Essentially when you are selecting the row to go to the next page you are trying to retrieve the data of that row onto the new page. Is this Assumption correct? If so then you have a number of options for you to use.

    Again you could use the Session Variables to store the data of the row once 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 you get the data from the row. You then store this data in session variables and you can use the following to find the data from the next 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 of using Query Strings to pass the data. Although for this method I believe you will have to change the Server.Transfer("Patientstaticformatrix.aspx"); to be Response.Redirect("Patientstaticformatrix.aspx");

    Below is an example on 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 to retrieve 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, however they are both slightly different. The Session Variable solution is probably the preferred method as it will stop users from being able to see all of the data passed (if you need to pass confidential information) where as the Query String values will be available to anyone who can see the URL.

    For more information on Session Variables and Query Strings see the below resources :

    ASP.NET Session State Overview

    Request.QueryString Collection

    0 讨论(0)
提交回复
热议问题