Click here to Skip to main content
Click here to Skip to main content

Get value from one page to another

, 21 Nov 2007
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Sun Rays
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 Pinmembersivacool23-Jan-10 23:14 
AnswerRe: clarification PinmemberSun Rays27-Jan-10 21:23 
GeneralBad Article PinmemberAnkit Rajpoot28-Jan-09 21:48 
GeneralGreat Artical Pinmember~V~28-Dec-07 23:46 
GeneralArticle Pinmembernorm .net21-Nov-07 3:08 
GeneralRe: Article PinmemberSun Rays21-Nov-07 18:13 

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 | Mobile
Web02 | 2.8.141022.2 | Last Updated 21 Nov 2007
Article Copyright 2007 by Sun Rays
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid