Click here to Skip to main content
Click here to Skip to main content

Post To Another Page in ASP.NET 2.0

, 14 Jul 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Atilla Ozgur
ISKUR
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

 
QuestionURL REDIRECTION PinmemberMember 917112827-Sep-12 5:23 
GeneralPost to different domain Pinmembercodegalaxy1-Jun-07 6:48 
GeneralNice Work Mate PinmemberUntitled84211-Apr-07 7:01 
QuestionWhat if i want to validate Textbox using Javascript. PinmemberMrinal Jaiswal11-Dec-06 0:59 
AnswerRe: What if i want to validate Textbox using Javascript. Pinmembernaraj27-Aug-07 23:36 
GeneralAwesome Pinmemberannesh@intermap.co.za15-Sep-06 4:58 
GeneralMay course High Coupling Design! Pinsussfrankie wai14-Jul-05 18:10 
GeneralRe: May course High Coupling Design! PinmemberAtilla Ozgur15-Jul-05 4:21 
GeneralPostToAnotherPage_src.zip' doesn't exist Pinmemberortad14-Jul-05 6:22 
GeneralRe: PostToAnotherPage_src.zip' doesn't exist PinmemberAtilla Ozgur15-Jul-05 4:18 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 14 Jul 2005
Article Copyright 2005 by Atilla Ozgur
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid