Tuesday, August 2, 2011

Asp.Net: Passing Gridview control from one page to another page

Hi,
    In this post, i will explain how to get gridview control defined in one page into another page. This is accomplished with "Page.PreviousPage" property.
    Let Page1.aspx contains a gridview control and we want this gridview (or any object) be passed to another page called page2.aspx.
We try this goal in two alternatives ways:

  • Button with PostBackUrl="Page2.aspx" attribute.

  • Server.Transfer("Page2.aspx", true); by coding.


In page1.aspx define:

  • Gridview control named - grid

  • Button with postbackUrl="page2.aspx" - btn1

  • and another Button with click-event - btn2


Markup:

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" />
<hr/>
<asp:Label runat="server" Text="Testing Gridview access to next-Page, try below buttons"></asp:Label>
<br />
<asp:Button ID="btn1" runat="server" PostBackUrl="Page2.aspx" Text="NextPageDirect" />
(or) serverTransfer:
<asp:Button ID="btn2" runat="server" Text="NextPageAtServer" OnClick="btn2_Click" />

Code:

//Testing Gridview data access from one page to next page.
public partial class Page1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGrid(); //Your logic to fill gridview
}
}

public GridView Getgrid
{
get { return this.grid; } //ViewState should be enabled for gridview.
}

protected void btn2_Click(object sender, EventArgs e)
{
Server.Transfer("Page2.aspx", true);
}
}



In Page2.aspx define:
Form control (default markup added by VS)
Code:

public partial class Page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page1 pg = Page.PreviousPage as Page1;
if (pg != null)
{
GridView grid = pg.Getgrid;
form1.Controls.Add(grid);
}
}
}

Now Open the Page1.aspx from browser (or, run from VisualStudio).



Above is the screenshot of Page1.aspx




Page2 screenshot after button click in Page1



Using the above technique and custom public properties, we can access any object from page1 to page2.

10 comments:

  1. Its working thanks for the code...

    ReplyDelete
  2. hey could you post this code in vb??I can't get a hint about "Page1 pg = page.PreviousPage as Page1"
    Thx

    ReplyDelete
    Replies
    1. Sure, i have used online C# to VB.Net conversion tool from:
      http://www.developerfusion.com/tools/convert/csharp-to-vb/

      and the result is like this, try it:
      Dim pg As Page1 = TryCast(page.PreviousPage, Page1)

      Delete
  3. Where did you get the Name "Page1" in "Page1 pg = Page.PreviousPage as Page1;" written in the Page_Load of Page2.aspx? I cannot seem to make it work. Thanks.

    ReplyDelete
    Replies
    1. Page1 is a class with public access modifier, so it is a accessible type.
      Try downloading the provided sample, give it a Run/execute, then compare with your code. Thanks.

      Delete
  4. Hi, Nice and Simple code for Asp.Passing Gridview control from one page to another page in asp.net c#

    -Aparna
    Theosoft

    ReplyDelete
  5. Hi, Can u guide me, if i want to retrieve a particular cell value of grid view and transfer it to another web form. then how can i do it. In simple terms- i want to retrieve quantity and total from grid view which is showing shopping cart result. how may i do it plz guide me.

    ReplyDelete
    Replies
    1. Hi Alam, You can retreive particular cell value by using jQuery, then use Ajax request to handler(ashx) or pageMethod passing these values. At server, save these values into session. Or, without ajax, save them in hiddenField and invoke button click or call __doPostBack().

      Delete