Click here to Skip to main content
15,886,578 members
Articles / Web Development / ASP.NET
Tip/Trick

Postback and Cross Page Posting in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.32/5 (13 votes)
10 Jun 2013CPOL3 min read 127.3K   7   8
Postback and cross page posting in ASP.NET

Introduction

Today, I am going to tell you about postback and cross page posting. Every developer who is working on ASP.NET has to do this thing in his project. This is an important part of the project and that is posting one page to another. Whenever you develop a website, you have a requirement to post to other pages. But ASP.NET pages postback to themselves in order to process events. For example, you have a data entry page and when you fill this page and click on submit button, then after saving the data, this page posts back to itself.

You will have to know the difference between postback for the first time when the new page is loaded and postback for the second time. So postback is just posting back to the same page. Remember one thing, the postback contains all the information collected on the Initial page for processing.

Now the question is this... how can you know whether the page is postback for the first time or not. The ASP.NET Page class has a property IsPostBack. You can check this using IsPostBack.

Here is the code for this:

C#
If(Page.IsPostBack==true) 
{
//Do whatever you want
}

So I hope you know now what is the postback. So let's discuss now the cross-page posting.

Cross Page Posting

Sometimes, you need to deal with the first webpage’s control value and you can do this with cross page posting. So, cross page posting enables you to post the WebPage and WebPage’s control values to another WebPage.

Let us try a simple example for this. Take two pages in your Website.

In first page, add:

  • One Textbox
  • One Calendar
  • One Label
  • Two Button

Here is the webpage source.

Webform1

HTML
<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication06102013.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Enter your name:<asp:TextBox ID="TextBox1" 
    runat="server"></asp:TextBox><br /><br />
    Select your DateOfBirth<asp:Calendar ID="Calendar1" 
    runat="server"></asp:Calendar><br />
    <br />
        <asp:Button ID="Button1" runat="server" 
        Text="Submit Page to Itself"
            onclick="Button1_Click" /> <asp:Button
            ID="Button2" runat="server" 
            Text="Submit Page to Webform2.aspx"
            PostBackUrl="~/WebForm2.aspx" />
  
        <br />
        <asp:Label ID="Label1" runat="server" 
        Text="Label"></asp:Label>
       </div>
    </form>
</body>
</html>

Go to the properties of button2 and select the PostBackUrl property of button and add the url of the second webform.

Now double click on button1 and add some code into the CS file.

Webform1.aspx.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApplication06102013 
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "Hello" + TextBox1.Text + 
            "<br/>" + "Your date of birth is" + 
            Calendar1.SelectedDate.ToShortDateString();
        }
    }
}

Now it's time for the second webform.

WebForm2.aspx

Add one label on page:

HTML
<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication06102013.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" 
        Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

Go the Load event of the page and add the below code into the page:

C#
using System;
using System.Collections.Generic;
using System.Linq; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApplication06102013
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox textbox2;
            Calendar calender2;
            textbox2 = (TextBox)PreviousPage.FindControl("TextBox1");
            calender2 = (Calendar)PreviousPage.FindControl("Calendar1");
            Label1.Text = "Hello" + textbox2.Text + "<br/>" + 
            " Your date of birth is " + calender2.SelectedDate.ToShortDateString();
 
        }
    }
}

Read this code carefully. There are two important things that you have to fix in your mind. The first thing is here is a property PreviousPage this property Gets the page that transferred Control to the current page means it Gets the webform1.aspx which is transferring control to webform2. And the second thing is FindControl() method. The string value assigned in findcontrol() method is the id of the control from the previous page. When these values are assigned, you can work with the values of control from the previous page. So guys, this is the demonstration of cross page posting. Do practice and know how it works.

Last Words

Currently, I am posting my articles for beginners. If you are experienced or a beginner, and you read this article then please give your feedback. Your feedback is important for me because only you can encourage me to post new tips in future. Post your comments and tell me what you think about this tip.

License

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


Written By
Software Developer CYBER DYNE PVT LIMITED
India India
Azad Chauhan is currently working as a software developer in Konasth E-Services Limited. He have 2+ years experience in asp.net,C#,Sql Sever. He currently working in a Grand Project which will Launch in the Mid of August 2013

Azad Chauhan's Blog

http://www.azadtechnologies.com/

Comments and Discussions

 
QuestionNice Demo. Pin
Gajanans16-Sep-15 0:01
Gajanans16-Sep-15 0:01 
QuestionGreat Post -- except it doesn't work at all for me... Pin
llahman27-May-15 15:35
professionalllahman27-May-15 15:35 
GeneralThanks a lot for this post. Pin
Santosh K. Tripathi22-Feb-15 23:03
professionalSantosh K. Tripathi22-Feb-15 23:03 
Questionchange the textbox value in webform1.aspx from webform2.aspx Pin
Bob7125-Jan-15 3:28
Bob7125-Jan-15 3:28 
QuestionNice Article Pin
Sam N R28-Mar-14 21:16
Sam N R28-Mar-14 21:16 
QuestionA Multiple Selection Dropdown Control with search Pin
abu2823-Jan-14 0:03
abu2823-Jan-14 0:03 
GeneralMy vote of 4 Pin
newton.saber10-Jun-13 7:42
newton.saber10-Jun-13 7:42 
GeneralRe: My vote of 4 Pin
AZAD CHOUHAN10-Jun-13 18:34
AZAD CHOUHAN10-Jun-13 18:34 

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.