Receiving the Data
The receiving page, ReceivingPage.aspx, is executed after the user clicks the
"Send Data Grid" button from SendingPage.aspx. In ReceivingPage.aspx
we must specify that we are expecting data to be passed in from another page. This
is done by adding a @Reference directive at the top of ReceivingPage.aspx.
This @Reference directive should follow the @Page directive and should
specify the ASP.NET Web page where we are expecting data from:
With the addition of this directive and some code, ReceivingPage.aspx
can access the shared information from the sending page, SendingPage.aspx.
To accomplish this we first must retrieve an instance of the HTTP
request from the Handler property of the Context object. The
handler provides us information of the page being requested. Since we called ReceivingPage.aspx
via a Server.Transfer the context of the SendingPage.aspx page was passed
along. Therefore, when we get the current handler, we are getting the handler for
SendingPage.aspx, meaning we can grab the data needed.
Essentially, we'll get the handler back from the Context object and cast it to an
instance of the SendingPage class (the ClassName specified in the
@Page directive in SendingPage.aspx). To aid us, we'll first declare
a "global" variable of type SendingPage in the server-side script, and then, in our
Page_Load event handler, assign this variable to the context's handler (assuming we've
not done a postback (because then we'd be getting the handler for the ReceivingPage.aspx page):
<script runat="server">
Dim objSendingPage As SendingPage
Sub Page_Load()
If Not IsPostBack Then
objSendingPage = CType(Context.Handler, SendingPage)
End If
End Sub
...
After the first page load, the objSendingPage variable contains an instance of the
class encapsulating the information from SendingPage.aspx. We can now use the variable
to access the properties of the class that contain the DataGrid's DataSource!
Imagine that on ReceivingPage.aspx we had a DataGrid named receivingDG.
We could populate it with the values from the SendingPage's DataGrid like so:
The following shows the complete code for ReceivingPage.aspx:
<%@ Page Language="VB" %>
<%@ Reference Page="SendingPage.aspx" %>
<script runat="server">
Dim objSendingPage As SendingPage
Sub Page_Load()
If Not IsPostBack Then
objSendingPage = CType(context.Handler, SendingPage)
End If
receivingDG.DataSource = objSendingPage.GridData
receivingDG.DataBind()
End Sub
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<b>Visiting ReceivingPage.aspx</b>
<asp:DataGrid id="receivingDG" runat="server" />
</form>
</body>
</html>
Examining the Needed Syntax for Code-Behind Pages
In the sending page you do not have to add any extra code. Just create the code-behind page as you
normally would.
As we have discussed earlier, on the receiving page we referenced the
class declared in the sending page by adding a @Reference directive at the
top of the page with the Page attribute set to the sending page.
With code-behind files use of the @Reference directive is optional. This is
due to Visual Studio .NET, which creates a single assembly (.dll)
per application, which includes all the individual classes of different web
forms that is automatically placed in the /Bin folder of the application.
We can retrieve an instance of the handler that first received the HTTP
request from the Handler property of the Context object in the same way we
did for server-side script version. The code in the ReceivingPage class would look
as follows:
Public objSendingPage As sendingPage
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
objSendingPage = CType(context.Handler, SendingPage)
End If
receivingDG.DataSource = objSendingPage.GridData
receivingDG.DataBind()
End Sub
Summary
One thing to keep in mind is that the
code samples shown above can also be implemented to transfer data from
different server controls like: TextBoxes, Lebels, RadioButtonLists,
CheckboxLists, Dropdownlists, DataGrids, Datalists etc. Also you can transfer more than one
piece of data per page - just add a public readonly property in the sending page for each datum
you wish to transfer.
Please feel free to contact me
or for any queries, ideas, or suggestions! Be sure to view the
live demo!
About the Author
Currently working on ASP.Net/VB.Net as Senior Developer at i-Vantage India
(P) Ltd., Secunderabad, India, which is an Offshore Development Center for
i-vantage Inc, Cambridge, (www.i-vantageinc.com). He is
a Bachelor of Engineering in Production Engineering and certified by Microsoft (MCP) and
Brainbench.com (ASP 3.0). He can be reached at tri@i-vantage.com.