65.9K
CodeProject is changing. Read more.
Home

Postback and Cross Page Posting in ASP.NET

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.32/5 (13 votes)

Jun 10, 2013

CPOL

3 min read

viewsIcon

128995

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:

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

<%@ 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

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:

<%@ 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:

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.