In this article I’ll explain how to pass ASP.Net GridView control from one page to another.
        
        
            The easiest way that comes to mind of any person would be to make use of Session variable and store the GridView object in Session variable and access it on the other page. But using Session has a disadvantage that it is stored in server memory i.e. RAM and it is directly proportional to the number of users accessing the site at a given point of time.
        
        
            For example if size of the Session object is 100 KB and 1000 users are currently accessing the site then the total memory used would be 100 x 1000 KB.
        
        
            Hence I am suggesting a better way of doing the same i.e. using the 
CrossPagePostBack property of ASP.Net which allows you to access the Previous Page and its controls.
        
            1. By setting 
PostBackUrl property of ASP.Net Button, LinkButton and ImageButton.
             
        
        
             
        
        
            Database
        
        
            Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
        
        
        
             
        
        
             
        
        
            HTML Markup
        
        
            I have added an ASP.Net GridView and (two buttons that will be used to redirect to the other page) onto my page as shown below.
        
        
            
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Font-Names="Arial"
            
            
                Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green">
            
            
                    <Columns>
            
            
                          <asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="CustomerID" />
            
            
                          <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
            
            
                          <asp:BoundField ItemStyle-Width="150px" DataField="Country" HeaderText="Country" />
            
            
                          <asp:BoundField ItemStyle-Width="150px" DataField="PostalCode" HeaderText="PostalCode" />
            
            
                    </Columns>
            
            
                    <AlternatingRowStyle BackColor="#C2D69B" />
            
            
                </asp:GridView>
            
            
                <asp:Button ID="Button1" runat="server" Text="Button1" PostBackUrl = "~/Page2.aspx" />
            
            
                <asp:Button ID="Button2" runat="server" Text="Button2" OnClick = "Redirect" />
            
         
         
         
        
            Namespaces
        
        
            You will need to import the following namespaces.
        
        
            C#
        
        
            
                using System.Data;
            
            
                using System.Configuration;
            
            
                using System.Data.SqlClient;
            
         
        
             
        
        
            VB.Net
        
        
            
                Imports System.Data
            
            
                Imports System.Configuration
            
            
                Imports System.Data.SqlClient
            
         
        
             
        
        
             
        
        
            Populating the GridView with records from 
        
        
            The GridView is populated with records from the Customers Table of the Northwind database inside the Page load event.
        
        
            C#
        
        
            
                protected void Page_Load(object sender, EventArgs e)
            
            
                {
            
            
                    if (!IsPostBack)
            
            
                    {
            
            
                        this.BindGrid();
            
            
                    }
            
            
                }
            
            
                 
            
            
                private void BindGrid()
            
            
                {
            
            
                    string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            
            
                    using (SqlConnection con = new SqlConnection(constr))
            
            
                    {
            
            
                        using (SqlCommand cmd = new SqlCommand())
            
            
                        {
            
            
                            cmd.Connection = con;
            
            
                            cmd.CommandText = "select top 10 * from customers";
            
            
                            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            
            
                            {
            
            
                                DataTable dt = new DataTable();
            
            
                                sda.Fill(dt);
            
            
                                GridView1.DataSource = dt;
            
            
                                GridView1.DataBind();
            
            
                            }
            
            
                        }
            
            
                    }
            
            
                }
            
         
        
             
        
        
            VB.Net
        
        
            
                Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
            
            
                    If Not IsPostBack Then
            
            
                        Me.BindGrid()
            
            
                    End If
            
            
                End Sub
            
            
                 
            
            
                Private Sub BindGrid()
            
            
                    Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
            
            
                    Using con As New SqlConnection(constr)
            
            
                        Using cmd As New SqlCommand()
            
            
                            cmd.Connection = con
            
            
                            cmd.CommandText = "select top 10 * from customers"
            
            
                            Using sda As New SqlDataAdapter(cmd)
            
            
                                Dim dt As New DataTable()
            
            
                                sda.Fill(dt)
            
            
                                GridView1.DataSource = dt
            
            
                                GridView1.DataBind()
            
            
                            End Using
            
            
                        End Using
            
            
                    End Using
            
            
                End Sub
            
         
         
         
        
            Redirecting to Other page using CrossPagePostBack
        
        
            In order to perform CrossPagePostBack, Button1 has been set with PostBackUrl while Button2 uses Server.Transfer method.
        
        
            C#
        
        
            
                protected void Redirect(object sender, EventArgs e)
            
            
                {
            
            
                    Server.Transfer("~/Page2.aspx");
            
            
                }
            
         
        
        
            VB.Net
        
        
            
                Protected Sub Redirect(ByVal sender As Object, ByVal e As EventArgs)
            
            
                   Server.Transfer("~/Page2.aspx")
            
            
                End Sub
            
         
         
         
        
            Handling the CrossPagePostBack and finding the GridView control
        
        
            On the destination page, the previous page’s reference is available in the Page.PreviousPage property. The control i.e. GridView can be easily accessed using the FindControl method.
        
        
            C#
        
        
            
                protected void Page_Load(object sender, EventArgs e)
            
            
                {
            
            
                    if (this.Page.PreviousPage != null)
            
            
                    {
            
            
                        GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
            
            
                    }
            
            
                }
            
         
        
        
            VB.Net
        
        
            
                Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            
            
                        If (Not (Me.Page.PreviousPage) Is Nothing) Then
            
            
                            Dim GridView1 As GridView = CType(Me.Page.PreviousPage.FindControl("GridView1"), GridView)
            
            
                        End If
            
            
                End Sub
            
         
         
         
        
            Accessing GridView when using Master Pages
        
        
            Now when Master Pages are being used, first the reference of ContentPlaceHolder is determined and then control i.e. GridView can be easily accessed using the FindControl method.
        
        
            C# 
        
        
            
                protected void Page_Load(object sender, EventArgs e)
            
            
                {
            
            
                    if (this.Page.PreviousPage != null)
            
            
                    {
            
            
                        Control ContentPlaceHolder1 = this.Page.PreviousPage.Master.FindControl("ContentPlaceHolder1");
            
            
                        GridView GridView1 = (GridView)ContentPlaceHolder1.FindControl("GridView1");
            
            
                    }
            
            
                }
            
         
        
        
            VB.Net
        
        
            
                Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            
            
                        If (Not (Me.Page.PreviousPage) Is Nothing) Then
            
            
                            Dim ContentPlaceHolder1 As Control = Me.Page.PreviousPage.Master.FindControl("ContentPlaceHolder1")
            
            
                            Dim GridView1 As GridView = CType(ContentPlaceHolder1.FindControl("GridView1"), GridView)
            
            
                        End If
            
            
                End Sub
            
         
        
             
        
        
             
        
        
            Downloads