An ASP.NET Blog
I work for Microsoft and help people and businesses make better use of technolgy to realize their full potential. The opinions mentioned herein are solely mine and do not reflect those of my employer.

ASP.NET 2.0 : Accessing controls in Previous Page

Monday, April 25, 2005 8:02 AM
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.

Feedback

# re: Whidbey: Accessing controls in Previous Page

Very usefull Example


Thanks & Regards
Srinivasrao
Cell:91 9985178877 1/24/2008 9:52 PM | srinivasarao.Maragani

# re: Whidbey: Accessing controls in Previous Page

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 3/10/2008 6:16 AM | Will

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

Getting New Info with proper exmples is a good Idea Thanks! 6/1/2008 10:13 PM | Shilpa

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

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;
.....
} 6/2/2008 10:08 PM | Deepak

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

good for referance 7/9/2008 5:20 PM | vshal raut

Post a comment





 

Please add 5 and 3 and type the answer here: