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 with C# and VB.Net.
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
HTML Markup
Source
The HTML Markup consists of following controls:
GridView – For displaying data.
The GridView consists of three BoundField columns and one TemplateField column.
TemplateField – The TemplateField column consists of ItemTemplate.
ItemTemplate – The ItemTemplate contains of a Button.
The Button has been assigned with the following property:
CommandName – For specifying which Button was clicked.
Button – For redirecting to another page.
The Button has been assigned with an OnClick event handler.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
< asp:BoundField DataField=" CustomerID" HeaderText="Customer Id" />
<asp:BoundField DataField="City" HeaderText="City" />
< asp:BoundField DataField=" PostalCode" HeaderText="Postal Code" />
<asp:TemplateField>
<ItemTemplate>
< asp:Button ID=" btnSelect" runat="server" Text="Select" CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
< asp:Button ID=" btnSend" runat="server" Text="Send Selected Row" OnClick="Send" />
Destination
The HTML Markup consists of following control:
Label – For displaying selected record.
< asp:Label ID=" lblMessage" runat="server"></asp:Label>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Populating GridView using C# and VB.Net
Inside the Page_Load event handler, first the connection string is read from the Web.Config file and the SELECT query is defined.
Then, a connection to the database is established using the SqlConnection class.
The SqlDataAdapter object is initialized with the SqlCommand and using the Fill function, the DataTable is populated with the records from database.
Finally, the DataTable is assigned to the DataSource property of GridView and the GridView is populated.
C#
protected void Page_Load(object sender,EventArgs e)
{
if (!this.IsPostBack)
{
string sql = "SELECT CustomerID, City,PostalCode FROM Customers";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(sql, con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handles Me.Load
If Not Me.IsPostBack Then
Dim sql As String = "SELECT CustomerID, City,PostalCode FROM Customers"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(sql, con)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End If
End Sub
Pass the Selected GridView row to another page
When the Send Button is clicked, first a check is performed if a GridView Row is selected then the Page is redirected to another Page using Server.Transfer function.
And if not then appropriate message will be displayed in
JavaScript Alert Message Box using
RegisterStartupScript method.
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 (gvCustomers.SelectedRow != null)
{
Server.Transfer("~/Page2.aspx");
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert" , "alert('Please select a row.')" , true);
}
}
VB.Net
Protected Sub Send(ByVal sender As Object, ByVal e As EventArgs)
If gvCustomers.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 another Page
Inside the Page_Load event handler, a check is performed if the PreviousPage is not NULL.
Then, the GridView of the Previous Page is accessed using the PreviousPage property of Page and then its Selected Row values are fetched.
The message is set using string.Format method where the cell index value is passed.
Finally, the message is displayed in Label control.
C#
protected void Page_Load(object sender,EventArgs e)
{
if (this.Page.PreviousPage != null)
{
GridView gvCustomers = (GridView)this.Page.PreviousPage.FindControl("gvCustomers");
GridViewRow selectedRow = gvCustomers.SelectedRow;
string message = string.Format("<b>CustomerId</b>: {0}<br /><b>City</b>: {1}<br /><b>PostalCode</b>: {2}",selectedRow.Cells[0].Text,selectedRow.Cells[1].Text,selectedRow.Cells[2].Text);
lblMessage.Text = message;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Me.Page.PreviousPage IsNot Nothing Then
Dim gvCustomers As GridView = CType(Me.Page.PreviousPage.FindControl("gvCustomers"),GridView)
Dim selectedRow As GridViewRow = gvCustomers.SelectedRow
Dim message As String = String.Format("<b>CustomerId</b>: {0}<br /><b>City</b>: {1}<br /><b>PostalCode</b>: {2}",selectedRow.Cells(0).Text,selectedRow.Cells(1).Text,selectedRow.Cells(2).Text)
lblMessage.Text = message
End If
End Sub
Screenshot
Demo
Downloads