Click here to Skip to main content
5,790,650 members and growing! (19,319 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Intermediate License: The Code Project Open License (CPOL)

Passing information between pages -The .NET way

By Ansil

Article which demonstrates how to pass data between pages.
C#, Windows, .NET 1.0, .NET 1.1, .NET, ASP.NET, IIS, Visual Studio, IIS 5, VS.NET2002, VS.NET2003, Dev

Posted: 22 Sep 2004
Updated: 22 Sep 2004
Views: 111,794
Bookmarked: 35 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
28 votes for this Article.
Popularity: 3.93 Rating: 2.72 out of 5
8 votes, 28.6%
1
3 votes, 10.7%
2
3 votes, 10.7%
3
6 votes, 21.4%
4
8 votes, 28.6%
5

Introduction

For those ASP developers who are new to ASP.NET:

I am not going in detail about the Response.Redirect or Session variables because they both are the same like classic ASP. This article is for those developers who used POST method of Form in ASP to pass the Form variables from one page to another. Surprisingly, in ASP.NET, we cannot pass the Form variables from one page to another by posting or submitting the form using a submit button. I found out a little bit stranded at first. But soon I found out this new method of passing Form variables. (Who wants the old form submission now.)

The method of doing it

This is a bit complex but classy method of passing values across pages. Here, we expose the values we want to access in other pages as properties of the Page class. Here, we code extra properties that we want to access in another web form. However, the efforts are worth considering. This method is object oriented than earlier methods. The entire process works as follows:

  1. Create the web form with controls that carry values.
  2. Create property with Get procedures to return the values in the controls.
  3. Provide some button that posts the Form to another WebForm.
  4. In the button click event handler, call Server.Transfer method that will transfer execution to the specified WebForm.
  5. In the second Form, you can get a reference to the first Form instance by using ontext.Handler property and cast it to the source WebForm class.

Then, you will use the Get properties we created to access the control values.

Create a WebForm as shown below:

Sample screenshot

Write properties to access the values in the controls inside the source Page class:

public string Name
{
    get{return txtName.Text;}
} 
public string Email
{
    get{return txtEmail.Text;}
}
public string Phone
{
    get{return txtPhone.Text;}
}

On the button click event, write code:

private void btnSubmit_Click(object sender, System.EventArgs e)
{
    Server.Transfer("destination.aspx");
}

On the Page_Load of the destination Page, write the following code to access the public properties:

SourcePage pgSource;
pgSource= (SourcePage) Context.Handler; 
Response.Write(pgSource.Name + "<br>");
Response.Write(pgSource.Email + "<br>");
Response.Write(pgSource.Phone + "<br>");

History

  • Version 1.0

    I thank Mr. Alvin George for writing this small code for me, due to my lack of time.

    License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    About the Author

    Ansil


    Ansil hails from Trivandrum.
    He works for Microsoft india (R&D ).
    he blogs at http://ansilf.spaces.live.com/



    "First they ignore you;then they laugh at you ;then they fight you ;then you win "
    Occupation: Software Developer (Senior)
    Location: India India

    Other popular ASP.NET articles:

    Article Top
    Sign Up to vote for this article
    You must Sign In to use this message board.
    FAQ FAQ Noise ToleranceSearch Search Messages 
     Layout  Per page   
     Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
    GeneralSource files requiredmemberammar_shaker14:04 15 Dec '07  
    GeneralPassing data setmemberJarnail22:10 6 Aug '07  
    GeneralBefore Accessing Source Page in Dest. Add Referencememberahmarshi5:52 1 Aug '07  
    GeneralRe: Before Accessing Source Page in Dest. Add Referencemembervishalbpatil0:57 6 Mar '08  
    Generalpass values through viewstate between different pagesmember22:50 10 Jan '07  
    GeneralHow can i post ArraysmemberShayeb7:19 18 Sep '06  
    GeneralWould this be equally the same as...membertranzformerz13:25 20 Jun '05  
    GeneralRe: Would this be equally the same as...memberHugo Flores8:08 1 Jul '05  
    Generalweb farmsmemberjlittle5:22 11 Mar '05  
    GeneralRe: web farmsmemberkikoman814:16 23 Mar '05  
    GeneralPassing values between pages in asp.netsussjeynthi21:07 10 Dec '04  
    GeneralRe: Passing values between pages in asp.netmemberBabu A8:48 18 Sep '06  
    GeneralTransfer to framememberAubyone16:15 10 Oct '04  
    GeneralSourcePagemembermagister0:13 1 Oct '04  
    GeneralRe: SourcePagemembermagister0:15 1 Oct '04  
    GeneralRe: SourcePagememberJerry Walter9:01 15 Oct '04  
    GeneralOnLoad eventmemberChili Beanie8:01 23 Sep '04  
    GeneralRe: onload eventmemberAnsil18:50 23 Sep '04  
    GeneralForm Post in ASP.NETmemberJeff X. Chang12:17 22 Sep '04  
    GeneralRe: Form Post in ASP.NETmemberzoomba8:33 23 Sep '04  
    GeneralRe: Form Post in ASP.NETmemberJeff X. Chang21:20 19 Oct '04  
    GeneralThank You Sir May I Have Another?memberJohnDeHope35:04 22 Sep '04  
    GeneralRe: Thank You Sir May I Have Another?memberdnh8:37 22 Sep '04  
    GeneralRe: Thank You Sir May I Have Another?memberJohnDeHope38:49 22 Sep '04  
    GeneralRe: Thank You Sir May I Have Another?memberdnh12:41 22 Sep '04  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 22 Sep 2004
    Editor: Smitha Vijayan
    Copyright 2004 by Ansil
    Everything else Copyright © CodeProject, 1999-2009
    Web12 | Advertise on the Code Project