By Tribikram Rath
Introduction
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.)
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:
- Server-side Script Blocks - this method is accomplished by simply placing
<script runat="server">... code ...</script>in the.aspxpage, similar to as was done in classic ASP. - Code-Behind pages - this technique creates two separate files - an
.aspxpage for Web controls and a.vbor.csclass 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.aspxReceivingPage.aspx- is called fromSendingPage.aspx; reads in the data of the DataGrid created inSendingPage.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.
|
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:
|
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:
|
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.




