Back

Postback to Another Page in ASP.net 2.0

In ASP.net 1.1 a page can only postback to itself, although it is quite common to post to another page in classic ASP 3. In ASP.net 2.0 a page can post to another page.

To post to another page, you change the PostBackUrl setting in the form button as per the following:

PostBackUrl="Test.aspx" class="button" ID="btnTest" runat="server" Text="Test" />

The PostBackUrl attribute allows us to post to another page using JavaScript behind the scenes. You can retrieve the values from the page using the following:

if (PreviousPage != null)
{
    TextBox txtName = (TextBox)PreviousPage.FindControl("txtName");
    Response.Write(txtName.Text);
}

Alternatively:

protected PostToAnotherPageSenderCSharp_aspx prev;
protected void Page_Load(object sender, EventArgs e)
{
    if(PreviousPage != null)
    {
        prev = (PostToAnotherPageSenderCSharp_aspx)PreviousPage;
        this.txtCrossPage.Text = prev.PublicTxtName;
    }
}

In this method we cast the PreviousPage to an appropriate page, since we know which page posted to this page. If you use more than one page to post to a single page, you should check if the previous page is casteable to this page using is operator or cast using as operator and check if it is null. After casting since ASP.NET knows which page it is, we can reach its method and fields normally. But normally web controls are defined as protected therefore you cannot reach them. What you should do is create a simple public property as shown below to reach the fields. If intellisense does not work here do not worry, it works perfectly during runtime.

public  string PublicTxtName{
    get{return this.txtName.Text;}

We used the button postbackurl attribute to sent values from one page to another in our examples. What if we wanted to insert some data into the database and then take the inserted data’s identity value to sent this to another page.

We can use IsCrossPagePostBack property to control if our page is posted to another page. Before doing something we can insert our data into the database and post to another page:

if (IsCrossPagePostBack)
{
    this.txtName.Text = "CrossPagePostBack";
}

Or we can use the normal postback to itself without setting PostBackUrl attribute and use Server.Transfer:

protected void Button1_Click(object sender, EventArgs e)
{
    this.txtName.Text = "Click Handler Wordked";
    Server.Transfer("~/PostToAnotherPageReceiverCSharp.aspx", true);
}

The second argument in the Server.Transfer method allows us to preserve the querystring and form information.

Do not use these methods together. If you use PostBackUrl stick to it in that page.

When you use the Server.Transfer method, you may get some cryptic error messages, like "Error executing child request for ~/TrialPost.aspx." These occur because of web control error codes or due to a compile error in your page. Instead of showing you the second page stack trace and where the error had occurred, ASP.NET simply says an error has occurred. Try to load the page by itself to see your error. Because of this, I suggest that you use the default values for testing purposes and after you are satisfied with your page, remove the default values. Also if your page always needs to work with another page, i.e. before adding roles to your current user, you need to select the user and put this validation logic after you have tested your page.

I have included VB.NET and C# versions of these codes. In the C# example, I change value of the text field before posting to another page.

protected void Page_Load(object sender, EventArgs e)
{
    if (IsCrossPagePostBack)
    {
        this.txtName.Text = "CrossPagePostBack";
    }
}

Back


Make a Comment on this Article

Your Name:
Comment: