How to Create Cross Page Posting in Asp.Net



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.

ASP.NET by default submit the forms to the same pages, cross page posting is submitted the form to a different page. This is usually required when you are creating a multi page form to collect information from the user on each page. When moving from the source to the target page.

"To use cross-page posting, you have to use the "postBackUrl" attribute to specify the page we want to post".

To set a web form to post back to a different web form, in the source web form, set the PostBackURL property of a control that implements IButtonControl (eg. Button, ImageButton, LinkButton) to the target web form. When the user clicks on this button control, the web form is cross-posted to the target web form. No other settings or code is required in the source web form.

Create to Webpage name it “Cross Page Posting PostBack” and “Cross Page PostBack 2”.
In First Page Design Form like below.

<div>
FirstName:<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox><br />
<br />
Last Name: <asp:TextBox ID="txtLastName" 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="~/Cross Page Post Back 2.aspx" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" PostBackUrl="~/Cross Page Post Back 2.aspx" Text="Submit to Second Page" /><br />
</div>

On Code behind generate Button 1 Click event and Code for Print Label value on same page.

Protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Hello" + txtFirstName.Text + " " + txtLastName.Text +
"<br/>" + "Your date of birth is" + Calendar1.SelectedDate.ToShortDateString();
}

Go to the properties of button2 and select the PostBackUrl property of button and add the URL of the second web form.

Now generate two Property for First Name and Last Name for Postback URL.

Public TextBox pbTxtFirstName
{
   get{return txtFirstName;}
}
public TextBox pbTxtLastName
{
   get{return txtLastName;}
}

Now go to Second page and Drag Label Control in Form for getting values from First page.

<div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>

Now go to Code behind and Code in Page load event so we can get information from previous page.

Protected void Page_Load(object sender, EventArgs e)
{
 if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
 {
  TextBox txtPbFirstName = (TextBox)PreviousPage.FindControl("txtFirstName");
  TextBox txtPbLastName = (TextBox)PreviousPage.FindControl("txtLastName");
  Label1.Text = "Welcome " + txtPbFirstName.Text + " " + txtPbLastName.Text;
  Calendar calender2;
  calender2 = (Calendar)PreviousPage.FindControl("Calendar1");
  Label1.Text = "Hello" + txtPbFirstName.Text + " " + txtPbLastName.Text + "<br/>" +
   " Your date of birth is " + calender2.SelectedDate.ToShortDateString();
 }
 else
 {
   Response.Redirect("https://www.google.co.in/?gws_rd=ssl");
 }
}

In this form we will check getting information is coming from previous page or not.

PreviousPage! = null && PreviousPage.IsCrossPagePostBack

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.

Related Posts

Previous
Next Post »

2 comments

comments
April 27, 2017 at 11:12:00 AM GMT+5:30 delete

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.
Thanks for your informative article.
dot net training and placement in chennai | dot net training institute in velachery

Reply
avatar

Thanks for comments.....