Passing Data the .NET way |
Written on: May, 12th 2002. |
Introduction
We all are familiar with the Request Object, which can be nicely put into use to retrieve the values from the web form. But, we can only retrieve the values of the web form objects using the Request object. But what if we need to retreive the entire object from a different aspx page. In Classic ASP, passing an entire object from one page to another was possible with the help of either Session Objects or Application Object or we need to implement a custom way to transfer objects between ASP pages.
In ASP .NET, we have a wonderful feature which can be used to retrieve all the objects from one aspx to another aspx page. Remember, if we have a DataGrid or DataList in page1.aspx, then we can easily retrieve the entire DataGrid or Datalist from page2.aspx.
Things that we will be learning in this article
- How to Pass a single value from one aspx page to another?
- How to pass an object from one aspx page to another?
- Using the attribute, ClassName in the Page directive
- Working with the Reference directive
Scenario
Assume that, we have a page called source.aspx with one textbox and a button. When we click on the button, we are going to invoke a different page called destination.aspx. Our aim is to retrieve the entire texbox object from the destination.aspx.
Content of Source.aspx
<%@ Page Language="vb" Classname="Source" %>
<HTML> <HEAD> <title>Passing Data .NET way - by Das </title>
<script language="VB" runat="server">
Sub Transfer(sender As Object, e As EventArgs) Server.Transfer("destination.aspx") End Sub
Public ReadOnly Property GetFirstname() As String Get Return txtFirstName.Text End Get End Property
Public ReadOnly Property GetFirstnameObj() As System.Object Get Return txtFirstName End Get End Property
</script>
</head>
<body>
<h3 align=center>Passing Data the .NET way</h3> <form id="Form1" method="post" runat="server">
First Name: <asp:TextBox ID="txtFirstName" BackColor="#ff0000" ForeColor="#ffffff" Font-Bold="True" Runat="server" />
<asp:Button ID="btnSubmit" Text="Submit" OnClick="Transfer" Runat="server" />
</form>
</body> </HTML>
|
How it works?
There are two new things that we will learn in the above ASPX page. The first one is in the Page directive. You can see in the above code, that we have a Page directive. We have set the property ClassName as "Source". This is the first most important piece of code in the Source.aspx. The next one is the property that we have in the Server side script. We have declared two properties, one which returns the entire textbox object (GetFirstNameObj) and another one, which just returns the text property of the textbox (GetFirstName). We have a method called Transfer, which is invoked when the submit button is clicked. This method just transfers the controll to the destination page, which is nothing but, destination.aspx.
Please note that, we need to take care of three aspects in the source page, which are
- The ClassName property in the Page Directive
- Creating properties, so that, we can retrieve these properties from the destination page
- Invoking the destination page using, Server.Transfer
Content of Destination.aspx
<%@ Page Language="vb" %> <%@ Reference Page="source.aspx" %>
<HTML> <HEAD> <title>Passing Data .NET way - by Das </title>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim objSource As source
objSource = CType(context.Handler, source) Response.Write("First Name is : " & objSource.GetFirstnameObj.text & " ") Response.Write("ForeColor is : ") Response.Write(objSource.GetFirstnameObj.ForeColor) Response.Write(" BackColor is : ") Response.Write(objSource.GetFirstnameObj.BackColor) End Sub
</script>
</head>
<body>
<h3 align=center>Passing Data the .NET way</h3> <form id="Form1" method="post" runat="server">
</form>
</body> </HTML>
|
How it works?
We saw the content of our destination.aspx page. The second line in the above code is the reference directive. This directive acts as the backbone of accessing information from a different aspx page. The reference directive takes a property called Page, which is used to specify the source page. In our example, we are trying to retrieve the textbox object which is in source.aspx.
Now, we need to create an instance of the object Source. This source is nothing, but the classname of source.aspx. (Please see the page directive in source.aspx, where we have specified the classname). The statement, Dim objSource As source creates an instance of the object, source. Then the statement, objSource = CType(context.Handler, source) gets the context of the page, Source. Now, we can use the object, objSource to retrieve all the public properties which is in the source.aspx.
Now, we can either retrieve the property, GetFirstName or GetFirstNameObj. If we just need the content of the textbox, then we need to invoke the property, GetFirstName.
Please note that, we need to take care of three aspects in the destination page, which are
- The Reference Directive.
- Creating an instance of the ClassName which is declared in source.aspx.
- Getting the context of source.aspx using the Context.Hanlder method.
Test this Script
Download the code
Click here to download the Source.ASPX page
Click here to download the Destination.ASPX page
Conclusion
Thus we have discussed in details about retrieving information from one page to another. We saw the example of textbox. We can do the same thing for DataGrid, DataList, Buttons, RadioButtons or any Web Server Control. Hope this discussion benefits you.
Links
Textbox Web Server Control
Send your comments to das@aspalliance.com