Passing Data the .NET way
page 1 of 1
Published: 17 Oct 2003
Unedited - Community Contributed
Abstract
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.
by Jesudas Chinnathampi (Das)
Feedback
Average Rating: 
Views (Total / Last 10 Days): 14055/ 14

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
  1. How to Pass a single value from one aspx page to another?
  2. How to pass an object from one aspx page to another?
  3. Using the attribute, ClassName in the Page directive
  4. 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

  1. The ClassName property in the Page Directive
  2. Creating properties, so that, we can retrieve these properties from the destination page
  3. 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

  1. The Reference Directive.
  2. Creating an instance of the ClassName which is declared in source.aspx.
  3. 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        



User Comments

Title: Passing Data   
Name: Sunny
Date: 2012-04-25 7:24:55 AM
Comment:
fantastic. That example helped me a lot. Keep it up. Thank You
Title: Window Forms   
Name: Mirza
Date: 2010-06-24 4:42:00 PM
Comment:
Can any one tell me how to pass single or multiple values from datagrid placed on one window form to another window form text boxes.

Regards
Title: Why not....   
Name: Rob
Date: 2008-10-01 10:55:32 AM
Comment:
Not sure what framework you're using, but if it's 2.0 why not just use Page.PreviousPage? On the desintation page you could simply use Page.PreviousPage instead of HttpContext.Current.Handler to accomplish the same.
Title: Passing Data   
Name: Nirmal
Date: 2007-10-26 5:03:35 AM
Comment:
Hai! Very? NiceOne. I will try in c# with same Concept. But Some Error Msg Came. If it Possible to send the Code to my Personal Mail(callnirmalkumar@yahoo.co.in).

Its Very Useful for me. But I will try on C#. Please Help Me.
Error:CS0246: The type or namespace name 'Default' could not be found (are you missing a using directive or an assembly reference?)

How can I solve?
Title: Passing Data   
Name: Md.Abu Kausar
Date: 2007-05-12 6:41:53 AM
Comment:
Article is vvvvveryyyyyyyyy fantastic thank you so muchhhhhhhh.
Title: passing data the .net way   
Name: Renjith
Date: 2007-02-14 6:20:23 AM
Comment:
Superb...Article is very good.Thanks a lot for sharing your knowledge.
Title: passing data the .net way   
Name: Patel vipul
Date: 2007-02-07 5:43:01 AM
Comment:
You give thus article in vb.net but how we use in c#.net for passing the data. if you have plz give me code.
Title: Passing Data the .NET way   
Name: gabriel
Date: 2007-01-31 5:06:33 PM
Comment:
nice!It helped me a lot to finish a project!
1000 smiles for you!
Title: Nice Article   
Name: Amandeep Singh
Date: 2006-08-28 6:23:55 AM
Comment:
Very nice effort....
Keep it up!!
Cheers
Title: Amazing, Stupendous   
Name: Christopher Rannow
Date: 2006-06-28 8:45:28 AM
Comment:
Wonderful article. An easy way to pass processed data between pages, and servers even. Usefull in neumerous applications. Thank you for sharing your knowledge.
Title: Access Previous Page(Object) Data   
Name: Ajay Choudhary
Date: 2006-06-28 8:23:28 AM
Comment:
thank you very much....its ...simply best...you can use it in..other way too..
Title: Mr   
Name: A Person
Date: 2006-05-24 11:49:47 AM
Comment:
Great information!!!
Title: great   
Name: naveen
Date: 2006-05-24 1:25:31 AM
Comment:
simply great. i was looking for this information from a week and nowhere its as clear as this article.
Title: Sending Custom Objects   
Name: Prasad
Date: 2006-01-20 7:10:37 AM
Comment:
The above example is useful if we need to access the page objects(elements) from another page. But what if I need to access/send an custom object created in one page to another page?
Title: another way that's .NET way   
Name: sanjay
Date: 2005-12-15 2:57:15 AM
Comment:
it's very simple in .net

send the value using
response.redirect("page.aspx?variablename=" & fieldnm.text)

then in page.aspx
just get the value by using
request.querystring("variablename")

how easy in .net
Title: passing data the .net way   
Name: Rajaan
Date: 2005-11-09 3:22:13 AM
Comment:
very nice and useful explanation.
Title: DataGrid   
Name: Ali
Date: 2005-10-03 7:19:40 AM
Comment:
Well done.

How about sending one single value from a DataGid using the same technique?
Title: server.transfer   
Name: duke
Date: 2005-09-15 1:05:57 PM
Comment:
It is very helpful. Thank you so much.you are an angle.
Title: why we need asp.net?   
Name: ac
Date: 2005-07-10 10:15:22 PM
Comment:
I can't never understand that, we have a much easier way - asp - to pass data cross pages, why we need asp.net to do it in more complicated way?

A new thing does not mean a better thing. Why microsoft just keeps to change things?

The only reason is: this is their way to how to keep making money.
Title: passing data the .net way   
Name: Reza
Date: 2005-06-18 8:03:32 PM
Comment:
This article is fantastic! thank you so much for your expertise.

Reza
Title: Mr.   
Name: Muneeb Waseem
Date: 2005-05-20 2:02:54 AM
Comment:
Thank You So Muchhhhhhhhhhhhh ! You just solved me one problem which has been bugging me for full 2 days (this might be a sign how dumb I am....but neways ..THANK YOUUU!) :p
Title: Templates   
Name: Adriaan
Date: 2005-04-18 10:11:59 AM
Comment:
I'm using user controls as templates for the project i'm working on.

You can define a classname in those templates:
<%@ Control Language="VB" ClassName="Source" %>

..But it's not recognized in the target page:
Type 'Source' is not Defined


Is there a way to pass information from those templates (.ascx) to another page?
Title: passing data the .net way   
Name: Adriaan
Date: 2005-04-15 5:40:49 AM
Comment:
Very useful, thank you!
Title: Excelent!!   
Name: dario
Date: 2005-04-07 7:04:13 PM
Comment:
You have no idea, what a usefull was the article form me!!!
Thank you very very very much!!!
Title: pass information through multiple forms   
Name: Rajaram Mhadgut
Date: 2005-03-09 11:26:50 PM
Comment:
Hi,

Nice article.

But I want some code which can be used to passing information through multiple (more that 2 pages) forms and finally submit all data in last form.

Can you suggest any easier method.
Title: Mr   
Name: Tim
Date: 2005-02-21 5:00:42 AM
Comment:
Thanks for this tutorial, it was very helpful. Cheers
Title: passing data the .net way   
Name: prasad chintalapati
Date: 2005-01-23 11:51:34 AM
Comment:
this article is very useful. Thank you very much for providing such a great useful information.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 9:58:51 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search