Click here to Skip to main content
Licence 
First Posted 13 Dec 2006
Views 33,928
Bookmarked 42 times

Cross Page Postbacks on a GridView

By Siva Subramanian | 13 Dec 2006
This article shows how to do cross page postbacks in a ASP.NET GridView.

1

2

3
3 votes, 30.0%
4
7 votes, 70.0%
5
4.53/5 - 10 votes
μ 4.53, σa 0.85 [?]

Introduction

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">   
    <Columns>        
      <asp:ButtonField HeaderText="Username" 
        CommandName="Select" DataTextField="Username" 
        ButtonType="Link"/> 
     <asp:BoundField HeaderText="Date" DataField="Date" />
     <asp:BoundField HeaderText="Description" DataField="Description" />
     <asp:TemplateField HeaderText="Amount (£)" 
          HeaderStyle-HorizontalAlign="Right" 
          ItemStyle-HorizontalAlign="Right">
            <ItemTemplate>
                  <%#Convert.ToDouble(DataBinder.Eval(
                       Container.DataItem,"Amount")).ToString("F")%>
            </ItemTemplate> 
      </asp:TemplateField>    
</Columns>
</asp:GridView>

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 = 
       ((LinkButton)selectedRow.Cells[0].Controls[0]);

    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 
   VirtualPath="~/pages/search/transactions.aspx"%>

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: " + 
                   this.PreviousPage.SelectedUserName);
    ShowUserSummaryDetails(
       this.PreviousPage.SelectedUserName);
  }
}

(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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Siva Subramanian

Web Developer

United Kingdom United Kingdom

Member
Siva works as a Senior Consultant for Just Data Services UK Limited (www.justdataservices.com) and works primarily on C#, ASP.NET and related Microsoft Technogies. Originally from Kerala, India he likes spicy food and Indian music.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoors7:03 28 Jul '10  
GeneralWhy not just set a session variable or do a get query Pinmemberphilmee9514:22 4 Apr '07  
GeneralPostBackUrl PinmemberGargi Chakravarty5:45 16 Feb '07  
QuestionPage.IsCrossPagePostBack = false PinmemberFire Dog11:27 7 Feb '07  
AnswerRe: Page.IsCrossPagePostBack = false PinmemberCJ Hughes16:31 11 Mar '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120209.1 | Last Updated 13 Dec 2006
Article Copyright 2006 by Siva Subramanian
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid