Click here to Skip to main content
13,000,002 members (48,322 online)
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 13 Dec 2006

Cross Page Postbacks on a GridView

, 13 Dec 2006
Rate this:
Please Sign up or sign in to vote.
This article shows how to do cross page postbacks in a ASP.NET GridView.


Have you ever wondered how to do a cross-page postback on a GridView in ASP.NET 2.0? This article shows you how to.

Cross Page Postbacks on a GridView control

In ASP.NET 2.0, with the cross-page postbacks, it is possible to do a post back to a different .aspx page and retrieve the values of the fields of the first page that did the post back.

Thus, I recently had a requirement to display transactions of users on a page (let us call this transactions.aspx) as shown below:

Sample screenshot

Once you have clicked any of the username hyperlinks, it would show a summary of the user on a second page (let us call this usersummary.aspx) as shown below:

Sample screenshot

The above task is a classic example of a cross-browser postback where I do a postback from the GridView of my first page (transactions.aspx) and retrieve the value of the selected (or clicked!!!) user name in my second page (usersummary.aspx).

Let us examine the code below. We will first look at the code of the initial page and then the postback page.

FirstPage (transactions.aspx):

The code for my GridView control is fairly straightforward. Note that my Username column is a ButtonField of type Link and CommandName set to Select.

<asp:GridView ID="gvTransactions" runat="server" 

   AllowSorting="True" AutoGenerateColumns="False" AllowPaging="True"  

   PageSize="50"  OnRowCommand="gvTransactions_OnRowCommand">   
      <asp:ButtonField HeaderText="Username" 

        CommandName="Select" DataTextField="Username" 

     <asp:BoundField HeaderText="Date" DataField="Date" />
     <asp:BoundField HeaderText="Description" DataField="Description" />
     <asp:TemplateField HeaderText="Amount (£)" 



Also note from above that I have specified the OnRowCommand event which will fire when a row is clicked. Let us examine the event handler code for OnRowCommand.

protected void gvTransactions_OnRowCommand(object sender, 
          GridViewCommandEventArgs e)
  if (e.CommandName == "Select")
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow selectedRow = (GridViewRow)gvTransactions.Rows[index];

    LinkButton clickedLinkedButton = 

    SelectedUserName = clickedLinkedButton.Text;
    Server.Transfer("../../pages/search/usersummary.aspx", true);

The key statements in the above code is where I set a Page level property called SelectedUserName with the value selected from the GridView. This is a simple property defined at the Page level of the first page (transactions.aspx) as given below. Also, I do an explicit Server.Transfer into my postback page (usersummary.aspx).

private string _selectedUserName;

public string SelectedUserName
get { return _selectedUserName; }
set { _selectedUserName = value; }

Now let us look at the postback page (usersummary.aspx). This is a very simple page, and the mechanism of retrieving the value of the selected username (from the first page) is even more simple. Have a look.

PostbackPage (usersummary.aspx):

I first specify the PreviousPageType directive on the top as given below. This is the location of my first page which initiated the postback.

<%@ PreviousPageType 


The PreviousPageType directive provides a way to get strong typing against the previous page, and is accessed through the PreviousPage property.

Now inside the Page_Load event, I retrieve the value of the SelectedUsername from the first page using this.PreviousPage.SelectedUsername as given below:

protected void Page_Load(object sender, EventArgs e)
  if (this.PreviousPage != null)
    Response.Write("Bingo!!! I got the selected Username: " + 

(Please note that ShowUserSummaryDetails() is my own private function and its code is not relevant for this article.)

Isn't it simple?? Enjoy ASP.NET...Any questions, please feel free to ask.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Siva Subramanian
Web Developer
United Kingdom United Kingdom
Siva works as a Senior Consultant for Just Data Services UK Limited ( and works primarily on C#, ASP.NET and related Microsoft Technogies. Originally from Kerala, India he likes spicy food and Indian music.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
manoors28-Jul-10 6:03
membermanoors28-Jul-10 6:03 
GeneralWhy not just set a session variable or do a get query Pin
philmee954-Apr-07 13:22
memberphilmee954-Apr-07 13:22 
GeneralPostBackUrl Pin
Gargi Chakravarty16-Feb-07 4:45
memberGargi Chakravarty16-Feb-07 4:45 
QuestionPage.IsCrossPagePostBack = false Pin
Fire Dog7-Feb-07 10:27
memberFire Dog7-Feb-07 10:27 
AnswerRe: Page.IsCrossPagePostBack = false Pin
CJ Hughes11-Mar-10 15:31
memberCJ Hughes11-Mar-10 15:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170624.1 | Last Updated 13 Dec 2006
Article Copyright 2006 by Siva Subramanian
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid