Click here to Skip to main content
15,301,841 members
Articles / Web Development / ASP.NET
Posted 14 Jul 2005


38 bookmarked

Post To Another Page in ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
3.27/5 (18 votes)
14 Jul 2005CPOL3 min read
Post form field values to another page in ASP.NET 2.0.


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) 
        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";


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Atilla Ozgur
Turkey Turkey
I started programming in 1991 with Amiga 68000 Assembler. I am a web and database developer proficient in different languages and databases

Comments and Discussions

Member 917112827-Sep-12 4:23
MemberMember 917112827-Sep-12 4:23 
GeneralPost to different domain Pin
codegalaxy1-Jun-07 5:48
Membercodegalaxy1-Jun-07 5:48 
GeneralNice Work Mate Pin
Untitled84211-Apr-07 6:01
MemberUntitled84211-Apr-07 6:01 
QuestionWhat if i want to validate Textbox using Javascript. Pin
Mrinal Jaiswal10-Dec-06 23:59
MemberMrinal Jaiswal10-Dec-06 23:59 
AnswerRe: What if i want to validate Textbox using Javascript. Pin
naraj27-Aug-07 22:36
Membernaraj27-Aug-07 22:36 
GeneralAwesome Pin 3:58 3:58 
GeneralMay course High Coupling Design! Pin
frankie ngai14-Jul-05 17:10
Memberfrankie ngai14-Jul-05 17:10 
GeneralRe: May course High Coupling Design! Pin
Atilla Ozgur15-Jul-05 3:21
MemberAtilla Ozgur15-Jul-05 3:21' doesn't exist Pin
ortad14-Jul-05 5:22
Memberortad14-Jul-05 5:22 
GeneralRe:' doesn't exist Pin
Atilla Ozgur15-Jul-05 3:18
MemberAtilla Ozgur15-Jul-05 3:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.