Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Post To Another Page in ASP.NET 2.0

0.00/5 (No votes)
14 Jul 2005 1  
Post form field values to another page in ASP.NET 2.0.

Introduction

In ASP.NET 1.1 a page can only postback to itself, though it is a common technique to post to another page in classic ASP 3.0 and PHP. In ASP.NET 2.0 a page can post to another page.

<asp:TextBox> ID="txtName">

The PostBackUrl attribute in the Button control allows us to post to another page using JavaScript under the covers. When you post to another page in ASP.NET 2.0, you can reach out to the values in the page using the following methods:

  1. The first method:
    if (PreviousPage != null)
    {
        TextBox txtName = (TextBox) 
                 PreviousPage.FindControl("txtName");
        this.txtCrossPage.Text = txtName.Text;
    }

    PreviousPage property holds the posted page. We check if it is null and use the FindControl method to reach our control.

  2. The second method:
    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";
    }
}

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