When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Book Reviews
Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
Web Hosts
Author an Article
spgif spgif

ASP ASP.NET ASP FAQs Feedback topnav-right
Print this Page!
Published: Wednesday, May 8, 2002

Transferring the Datagrid Data Between Web Forms

By Tribikram Rath


In classic ASP there are many methods for transferring data from one ASP page to another Cookies, hidden input fields, query strings, and session variables are the most common approaches. (See the Maintaining Persistent Information on the Web sample chapter from Sams Teach Yourself ASP 3.0 in 21 Days for more information.) These same techniques can be used in ASP.NET, but since ASP.NET includes rich server side controls (like DataGrids, DataLists, etc.), passing data from one page to another via these classic approaches may not be feasible. This article discusses how to pass complex data from one ASP.NET Web page to another, specifically DataGrid data. (For more information on the DataGrid Web control be sure to read: An Extensive Examination of the DataGrid.)

- continued -

Code-Behind vs. Server-Side Script Blocks

If you've worked with ASP.NET you're probably aware that ASP.NET Web pages can be created in one of two ways:

  1. Server-side Script Blocks - this method is accomplished by simply placing <script runat="server">... code ...</script> in the .aspx page, similar to as was done in classic ASP.
  2. Code-Behind pages - this technique creates two separate files - an .aspx page for Web controls and a .vb or .cs class with VB.NET or C# code specifying the functionality of the page. These pages are compiled into DLLs before being deployed to the Web server. This is the default model used by Visual Studio .NET (you can also use code behinds without VS.NET)

In this article we will examine how to pass DataGrid data across page boundaries using both techniques.

Passing DataGrid Data using Server-Side Script Blocks

Let us consider two ASP.NET Web pages that are assigned the following duties:

  • SendingPage.aspx: Populates a DataGrid Web control with database data and presents the user with a form that, when submitted, redirects them to the second page, ReceivingPage.aspx
  • ReceivingPage.aspx - is called from SendingPage.aspx; reads in the data of the DataGrid created in SendingPage.aspx.

Sending the Data

I assume that most of the ASP programmers have an idea of ASP.NET web forms, hence I'm not going to spend time describing the entire page; instead, I'll focus just on the vital parts for data passing. (If you have ASP.NET questions be sure to post them on the ASP.NET forum on ASPMessageboard.com.) Let's examine the following chunk of code from the SendingPage.aspx. The code below creates a DataGrid populated with the data from the Authors table of the SQL Server pubs database.

<%@ Page Language="VB" ClassName="SendingPage" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
  Public ReadOnly Property GridData() As System.Object
      ' Datasource of Datagrid1 is a data table inside a dataset
      Return DataGrid1.DataSource
    End Get
  End Property

  Sub Button_Clicked (sender As Object, e As EventArgs)
  End Sub

  Sub Page_Load(sender As Object, e As EventArgs)
    'bind some relevant data to the grid at page load
  End Sub

  Sub GetData()
    Dim DS As System.Data.DataSet
    Dim Con As System.Data.SqlClient.SqlConnection
    Dim Ada As System.Data.SqlClient.SqlDataAdapter
    Dim connStr as String
    conStr = "server=localhost;uid=sa;pwd=;initial catalog=pubs"
    Con = New System.Data.SqlClient.SqlConnection(connStr)
    Ada = New System.Data.SqlClient.SqlDataAdapter(_
              "select * from authors", Con)
    DS = New System.Data.DataSet()
    Ada.Fill(DS, "Table1")    
    'Fill the DataGrid
    DataGrid1.DataSource = DS.Tables("Table1").DefaultView
  End Sub

  <form runat="server">
      <b>Visiting SendingPage.aspx</b>
      <asp:datagrid id=DataGrid1 runat=server />

      <asp:Button id="myButton" OnClick="Button_Clicked" 
            Text="Send Grid Data" runat=server />

Let's take a moment to analyze the above lines of code that are relevant to passing data from one page to another. When using server-side script blocks we first need to specify a class name for the ASP.NET Web page that contains the information to be sent to another page. This can be done by specifying the ClassName attribute in the @Page directive like so:

<%@ Page Language="VB" ClassName="SendingPage" %>

Next, we need to create a public property for the SendingPage class using a "get" accessor for each value we want to share with another page. This public readonly property returns the value we want to pass, such as text of a text box or label Web control. Since we wish to return the DataGrid's data, we return the DataSource property of our DataGrid:

  Public ReadOnly Property GridData() As System.Object
      ' Datasource of Datagrid1 is a data table inside a dataset
      Return DataGrid1.DataSource
    End Get
  End Property

To initiate the transfer of data from SendingPage.aspx to ReceivingPage.aspx the user must click the submit button; when clicked the Button_Clicked event handler is fired, which transfers control to ReceivingPage.aspx via a Server.Transfer. This stops the execution of the current page and passes it to the specified page. We initiate this transfer with the following code:

  Sub Button_Clicked(sender As Object, e As EventArgs)
  End Sub

Now that we've examined the code and structure for SendingPage.aspx, let's turn our attention to the makeup of the ASP.NET page that will receive the request. For more on the receiving page see Part 2.

  • Part 2!

  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article