Click here to Skip to main content
11,710,943 members (85,440 online)
Click here to Skip to main content

Passing information between pages -The .NET way

, 22 Sep 2004 CPOL 161.9K 40
Rate this:
Please Sign up or sign in to vote.
Article which demonstrates how to pass data between pages.

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)

    Share

    About the Author

    Ansil
    Software Developer (Senior)
    India India
    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 "

    You may also be interested in...

    Comments and Discussions

     
    Generalpassing multiple values between web form Pin
    syed abdul kather25-Feb-10 7:41
    membersyed abdul kather25-Feb-10 7:41 
    GeneralSource files required Pin
    ammar_shaker15-Dec-07 13:04
    memberammar_shaker15-Dec-07 13:04 
    GeneralPassing data set Pin
    Jarnail6-Aug-07 21:10
    memberJarnail6-Aug-07 21:10 
    GeneralBefore Accessing Source Page in Dest. Add Reference Pin
    ahmarshi1-Aug-07 4:52
    memberahmarshi1-Aug-07 4:52 
    GeneralRe: Before Accessing Source Page in Dest. Add Reference Pin
    vishalbpatil5-Mar-08 23:57
    membervishalbpatil5-Mar-08 23:57 
    Generalpass values through viewstate between different pages Pin
    Member #362219710-Jan-07 21:50
    memberMember #362219710-Jan-07 21:50 
    QuestionHow can i post Arrays Pin
    Shayeb18-Sep-06 6:19
    memberShayeb18-Sep-06 6:19 
    GeneralWould this be equally the same as... Pin
    tranzformerz20-Jun-05 12:25
    membertranzformerz20-Jun-05 12:25 
    GeneralRe: Would this be equally the same as... Pin
    Hugo Flores1-Jul-05 7:08
    memberHugo Flores1-Jul-05 7:08 
    Generalweb farms Pin
    jlittle11-Mar-05 4:22
    memberjlittle11-Mar-05 4:22 
    GeneralRe: web farms Pin
    kikoman823-Mar-05 13:16
    memberkikoman823-Mar-05 13:16 
    GeneralPassing values between pages in asp.net Pin
    jeynthi10-Dec-04 20:07
    sussjeynthi10-Dec-04 20:07 
    GeneralRe: Passing values between pages in asp.net Pin
    Babu A18-Sep-06 7:48
    memberBabu A18-Sep-06 7:48 
    GeneralTransfer to frame Pin
    Aubyone10-Oct-04 15:15
    memberAubyone10-Oct-04 15:15 
    GeneralSourcePage Pin
    magister30-Sep-04 23:13
    membermagister30-Sep-04 23:13 
    GeneralRe: SourcePage Pin
    magister30-Sep-04 23:15
    membermagister30-Sep-04 23:15 
    GeneralRe: SourcePage Pin
    Jerry Walter15-Oct-04 8:01
    memberJerry Walter15-Oct-04 8:01 
    GeneralOnLoad event Pin
    Chili Beanie23-Sep-04 7:01
    memberChili Beanie23-Sep-04 7:01 
    GeneralRe: onload event Pin
    Ansil23-Sep-04 17:50
    memberAnsil23-Sep-04 17:50 
    GeneralForm Post in ASP.NET Pin
    Jeff X. Chang22-Sep-04 11:17
    memberJeff X. Chang22-Sep-04 11:17 
    GeneralRe: Form Post in ASP.NET Pin
    zoomba23-Sep-04 7:33
    memberzoomba23-Sep-04 7:33 
    GeneralRe: Form Post in ASP.NET Pin
    Jeff X. Chang19-Oct-04 20:20
    memberJeff X. Chang19-Oct-04 20:20 
    QuestionThank You Sir May I Have Another? Pin
    JohnDeHope322-Sep-04 4:04
    memberJohnDeHope322-Sep-04 4:04 
    AnswerRe: Thank You Sir May I Have Another? Pin
    dnh22-Sep-04 7:37
    memberdnh22-Sep-04 7:37 
    GeneralRe: Thank You Sir May I Have Another? Pin
    JohnDeHope322-Sep-04 7:49
    memberJohnDeHope322-Sep-04 7:49 
    GeneralRe: Thank You Sir May I Have Another? Pin
    dnh22-Sep-04 11:41
    memberdnh22-Sep-04 11:41 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web03 | 2.8.150819.1 | Last Updated 22 Sep 2004
    Article Copyright 2004 by Ansil
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid