Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Get value from one page to another

0.00/5 (No votes)
21 Nov 2007 1  
Getting value from one page to another page

Introduction

Sometimes we want to get value of any control of First page to another page. Here is an Article To do the same without using Session or Querysting.

If we are using some data which we dont want to show in addressbar (URL) at that time we are facing problem using Querystring, sometimes we are encrypting the URL and descrypting at another (next) page so anyone don't get exact meaning of passed querystring values, but here is best way to come out from those problems.

And using Session having some another problem because sessions are always stored at Server side. So server memory occupies.

Now here is good idea to overcome from above both the problems.

Using the code

In your application add 2 webpages.

First page ID = Page1.aspx

and Second Page ID = Page2.aspx

Now in Page1.aspx add below controls with defined properties.

TextBox : ID = txtFirstPage

Button : ID = btnSend

On button btnSend's Click event write below code :

//

// Any source code blocks look like this

//

protected void btnSend_Click(object sender, EventArgs e)
{
// Transfer first page to Second Page

// Always use Server.Transfer. Then and then you will get desire OuptPut

Server.Transfer("Page2.aspx");
}

Now on the Page2.aspx write below code in Page Load Event.

protected void Page_Load(object sender, EventArgs e)
{

if(Page.PreviousPage != null)
{
 TextBox txt =(TextBox) Page.PreviousPage.FindControl("txtFirstPage");
 if(txt != null)
 {
  Response.Write("<font color='Blue' size='15'> <b>" + txt.Text + "</b></font>");
 } 
}
}

Now Set Page1.aspx as startup page, run your application.

Enter any text in TextBox and then Submit the button.

Here it will Transfer Page1.aspx to Page2.aspx and on Page you will see your entered text of TextBox with Blue color !!

You can access any control of Page1.aspx to Page2.aspx.

Enjoy the coding.

Points of Interest

Always use Server.Transfer , if you are trying to use Response.Redirect then Page.PreviousPage will null always.

Keep coding and get the best code to achieve your goal.

Try Try and Try. There are always lots of way to do.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here