Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET
Article

Get value from one page to another

Rate me:
Please Sign up or sign in to vote.
1.25/5 (9 votes)
21 Nov 20071 min read 39.3K   9   6
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


Written By
Team Leader
India India
Working as a Team Lead in MNC located at India.
In IT world, I have worked with Microsoft Technologies and always ready for R&D.
Worked with Website Development and Windows based applications as well.


New errors are really good and being happy to resolve those.....

Comments and Discussions

 
Questionclarification Pin
sivacool23-Jan-10 23:14
sivacool23-Jan-10 23:14 
AnswerRe: clarification Pin
Sun Rays27-Jan-10 21:23
Sun Rays27-Jan-10 21:23 
GeneralBad Article Pin
Ankit Rajpoot28-Jan-09 21:48
Ankit Rajpoot28-Jan-09 21:48 
GeneralGreat Artical Pin
~V~28-Dec-07 23:46
~V~28-Dec-07 23:46 
GeneralArticle Pin
NormDroid21-Nov-07 3:08
professionalNormDroid21-Nov-07 3:08 
GeneralRe: Article Pin
Sun Rays21-Nov-07 18:13
Sun Rays21-Nov-07 18:13 

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

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