In this article I will explain with an example, how to pass or transfer a selected row of GridView control to another page along with its data and controls using Server.Transfer in ASP.Net using C# and VB.Net.
 
HTML Markup
Following HTML Markup consists of an ASP.Net GridView control with a Button to select the row. Also I have added a Button which will send the ASP.Net GridView Selected Row to the other page when clicked.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Font-Names="Arial"
    Font-Size="10pt">
    <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="PostalCode" HeaderText="PostalCode" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="btnSelect" runat="server" Text="Select" CommandName = "Select" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Button ID="btnSend" runat="server" Text="Send Selected Row" OnClick = "Send" />
 
 
Pass the Selected GridView row to Other page
When the Send button is clicked, first a check is performed whether the GridView has a Selected Row or not. If the GridView has a Selected Row, the Page is redirected to another Page using Server.Transfer function.
Note: Here Server.Transfer is used instead of Response.Redirect, since when Server.Transfer is used to redirect from one Page to another, the Previous Page and its controls are accessible using the PreviousPage property.
 
C#
protected void Send(object sender, EventArgs e)
{
    if (GridView1.SelectedRow != null)
    {
        Server.Transfer("~/Page2.aspx");
    }
    else
    {
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please select a row.')", true);
    }
}
 
VB.Net
Protected Sub Send(sender As Object, e As EventArgs)
    If GridView1.SelectedRow IsNot Nothing Then
        Server.Transfer("~/Page2.aspx")
    Else
        ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('Please select a row.')", True)
    End If
End Sub
 
Accessing the selected GridView Row on the other Page
Inside the Page Load event, the GridView of the Previous Page is accessed using the PreviousPage property and then it's Selected Row values are fetched.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (this.Page.PreviousPage != null)
    {
        GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
        GridViewRow selectedRow = GridView1.SelectedRow;
        Response.Write("CustomerId: " + selectedRow.Cells[0].Text + "<br />");
        Response.Write("City: " + selectedRow.Cells[1].Text + "<br />");
        Response.Write("PostalCode: " + selectedRow.Cells[2].Text);
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Me.Page.PreviousPage IsNot Nothing Then
        Dim GridView1 As GridView = DirectCast(Me.Page.PreviousPage.FindControl("GridView1"), GridView)
        Dim selectedRow As GridViewRow = GridView1.SelectedRow
        Response.Write("CustomerId: " + selectedRow.Cells(0).Text & "<br />")
        Response.Write("City: " + selectedRow.Cells(1).Text & "<br />")
        Response.Write("PostalCode: " + selectedRow.Cells(2).Text)
    End If
End Sub
 
 
Demo
 
 
Downloads