ASP.NET 2.0 : Accessing controls in Previous Page

Hi,

In my earlier
article, I explained how the PostBackUrl property has made the job of posting the page to a different page easy in Whidbey.

There is a more effective way of accessing the Controls in the previous page. Its using the PreviousPage property of the Page.

Say we have a page Default.aspx with a Textbox "Text1" and a Button "Button1".

There are two ways on how we can access the controls in Default.aspx from another page.

1. Setting the PostBackUrl property of the Button to the New Page, as follows:-

<asp:Button ID="button1" Runat=server Text="submit" PostBackUrl="~/NewPage.aspx" />

Then, in the NewPage.aspx, you can access the TextBox control on Default.aspx as follows:-


public void page_load()
{

TextBox tb = (TextBox)PreviousPage.FindControl("text1");
Response.Write(tb.Text);
}


Note that a new TextBox tb is declared and typecasted using the PreviousPage and FindControl of the ID of the Control in the Previous Page.

2. The other way is the old way of transferring the page to new page using Server.Transfer on the Click Event of the Button in the first page.

But in this case, the PostBackUrl property should not be specified for the Button control.

In the Button declaration in Default.aspx, the code should be

<asp:Button ID="button1" Runat=server Text="submit" OnClick="button1_Click" />

Then, in the codebehind,

void button1_Click(object sender, EventArgs e)
{
Server.Transfer("NewPage.aspx");
}

Still the PreviousPage.FindControl method would work.

This is a wonderful way of accessing the Controls in the Previous Page and I reckon it would be very useful for Developers.

Thanks.

Print | posted on Monday, April 25, 2005 8:02 AM

Comments on this post

# re: Whidbey: Accessing controls in Previous Page

Requesting Gravatar...
Very usefull Example


Thanks & Regards
Srinivasrao
Cell:91 9985178877
Left by srinivasarao.Maragani on Jan 24, 2008 9:52 PM

# re: Whidbey: Accessing controls in Previous Page

Requesting Gravatar...
Hi,

Very useful example. I'm having problems getting the values from the first page still.

I'm getting error:
Object reference not set to an instance of an object.

on the second page.

thanks,
Will
Left by Will on Mar 10, 2008 6:16 AM

# re: ASP.NET 2.0 : Accessing controls in Previous Page

Requesting Gravatar...
Getting New Info with proper exmples is a good Idea Thanks!
Left by Shilpa on Jun 01, 2008 10:13 PM

# re: ASP.NET 2.0 : Accessing controls in Previous Page

Requesting Gravatar...
Hi,
If U r using master page then PreviousPage.FindControl won't work directly. First we want to get the ContentPlaceHolder(we want to give the ContentPlaceHolder ID which is in the master page.) and from that object we want to do the find control.


if (PreviousPage != null)
{
ContentPlaceHolder cp = ((ContentPlaceHolder)PreviousPage.Master.FindControl("cphMain"));
string fname= ((TextBox)cp.FindControl("txtFname")).Text;
.....
}
Left by Deepak on Jun 02, 2008 10:08 PM

# re: ASP.NET 2.0 : Accessing controls in Previous Page

Requesting Gravatar...
good for referance
Left by vshal raut on Jul 09, 2008 5:20 PM

# re: ASP.NET 2.0 : Accessing controls in Previous Page

Requesting Gravatar...
super blog in the perect
Left by azdırıcı on Nov 30, 2008 5:03 PM

# re: ASP.NET 2.0 : Accessing controls in Previous Page

Requesting Gravatar...
good i like it
Left by mario oyunları on Sep 22, 2009 12:17 AM

# re: ASP.NET 2.0 : Accessing controls in Previous Page

Requesting Gravatar...
ALIN GÜZELLL
Left by meksika biberi zayıflama hapı on May 31, 2010 11:11 PM

# re: ASP.NET 2.0 : Accessing controls in Previous Page

Requesting Gravatar...
Although moreover mistaken kindness me feelings do be marianne. Son over own nay with tell they cold upon are. Cordial village and settled she ability law herself. Finished why bringing but sir bachelor unpacked any thoughts. wow!
Left by converter on Sep 19, 2010 6:51 AM

# re: ASP.NET 2.0 : Accessing controls in Previous Page

Requesting Gravatar...
Nice piece of info. But I already know this. I was looking for some better.
Left by Rinkal on Oct 06, 2010 3:01 PM

# re: ASP.NET 2.0 : Accessing controls in Previous Page

Requesting Gravatar...
yes it still works
Left by inkasso on Dec 30, 2010 11:34 PM

# re: ASP.NET 2.0 : Accessing controls in Previous Page

Requesting Gravatar...
Thank you Harish and Deepak.
Left by Aqeel Ahmed on Jun 29, 2011 11:44 AM

# re: ASP.NET 2.0 : Accessing controls in Previous Page

Requesting Gravatar...
// To Get values from first page to second page

// Method 1
// On first page button click event use Server.Transfer("SecondPage.aspx");

//TextBox tb = (TextBox)PreviousPage.FindControl("txtFirstPage");
//Response.Write(tb.Text);

// if you are using response.redirect("SecondPage.aspx") then use PostBackUrl="SecondPage.aspx" as a button property mandatory.


//step 1 ==> On first page button put PostBackUrl="SecondPage.aspx"

// step 2 ==> on Second page get the first page control by following code





// Method 2

string t = Request.Form["txtFirstPage"];

//or
// Method 3
string t = Request["txtFirstPage"];
Left by TAPAN GUPTA on Aug 13, 2012 11:39 AM

Your comment:

 (will show your gravatar)