PostBack to a different page

Tutorial on how you can PostBack from one page to another. By default in ASP.NET 1.0 and 1.1 PostBack is done to the same page. We will see how we can get around this. Also, you will see how the PostBackUrl property in ASP.NET 2.0 makes this easier.

Post form to another page in ASP.NET 1.0 and 1.1

When posting a form in an ASP.NET page, it gets posted to itself. That means that when you press the submit button, the same page reloads, and you get the data using the if(IsPostBack) condition. But how about when you want to post the form to a different page. For example WebForm1.aspx has the form, and when the user presses the submit button you want it to be posted and the results to be shown in WebForm2.aspx. There’s no easy way to do this in ASP.NET 1.1 (or 1.0). However, let’s see first how this can be done in ASP.NET 1.x.

When the submit button is pressed, instead of doing PostBack, you need to use the following line so that the second WebForm is loaded:

private void btnSubmit_Click(object sender, System.EventArgs e)




We don’t use a normal redirection, instead we use Server.Transfer(), because we want the properties of WebForm1 to be accessible inside WebForm2. What properties? Well, we need to create properties for each element we have inside WebForm1 and we want to get the value of in WebForm2.

So, to be able to access the elements of WebForm1 from WebForm2, we need to set up properties inside WebForm1 for the textboxes and any other form controls we have. Suppose we only have a TextBox inside WebForm1, we set a property so we can be able to retrieve it from WebForm2:

public string EmailAddress




      return txtEmail.Text;



By defining this property we will be able to access the value of txtEmail from WebForm2. To get to this property from inside WebForm2, we first need to create a new instance of WebForm1:

WebForm1 WF1;

// Make sure the page is opened because of Server.Transfer()

// and not because it was accessed directly



   WF1 = (WebForm1 )Context.Handler;




   WF1 = null;


if(WF1 != null)




Using the try/catch block we make sure that the page is opened because PostBack was performed on WebForm1 (the referrer is WebForm1) and not because the URL to WebForm2 was typed directly in the browser. This way we prevent an ugly error that can occur in case the WebForm2 isn’t opened as a result of WebForm1 PostBack.

Post form to another page in ASP.NET 2.0

To test this, create a new ASP.NET Website, and another page so that you have a total of two pages: Default.aspx and Default2.aspx. Inside Default.aspx add a TextBox and a Button.

Now change Button1‘s PostBackUrl property to Default2.aspx. Now we only need to take care of Default2.aspx, in which the content of the TextBox from Default1.aspx will be shown.

Inside the Page_Load() event, this is the only code we need to use:

if (PreviousPage.IsCrossPagePostBack)


   TextBox TextBox1 = (TextBox)PreviousPage.FindControl("TextBox1");



We first check to see if we got PostBack from WebForm1.aspx. Then we use the FindControl() to get the TextBox1 control back from WebForm1.aspx, and we cast it to a TextBox. We then display its content on the screen using Response.Write().

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top