Thursday, 5 December 2013

What is Postback and Cross Page Postback in C#asp.net


What is Postback  and Cross Page Postback in C#asp.net:-
Now I am going to describe about Postback  and Crosspostback in asp.net.This is the  important part for the project.Actually this is very simple concept. Whenever you develop a website, you have a requirement to post to other pages also.
Postback:-
     Postback means the page posted back its self after operation.for an example…suppose you have a registration page and you filled this page than click to the ok button for save.
     After click the ok button the message showed you Your record saved successfully than you press ok button than after saving the data the page return to same page or your registration page in which page You ware filling the data but did not move to anather page.This concept is called postback
Hints:- Postback contains all  information collected on the Initial page for processing purpose.
     Now one question can arise in your mind that- How can you know whether the page is postback for the first time or not ?
Ans- The ASP.NET Page class has a property IsPostBack. You can check this using IsPostBack.



Plese note this code:-
If(Page.IsPostBack==true)
{
//Your statement
}


Cross page Postback:-

   Cross page postback is nothing but it is a concept which is responsible to used  submit one page (postback1.aspx) controls to another page (postback2.aspx) and access those page (postback1.aspx) control values in another page(postback2.aspx).

       Now I will describe about this concept with simple example .

      Now first create two web application pages  postback1.aspx and postback2.aspx.

Ø    Now open your postback1.aspx page and write this code :-

<html xmlns="http://www.w3.org/1999/xhtml">
 <head id="Head1" runat="server">
<title>Cross Page Postback Example in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td><b>Name:</b></td>
<td><asp:TextBox ID="txtName" runat="server"/></td>
</tr>
<tr>
<td><b>Address:</b></td>
<td><asp:TextBox ID="txtAddress" runat="server"/></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><asp:Button ID="btnPostback" Text="Postback" runat="server"
        PostBackUrl="~/postback2.aspx" BackColor="Red" ForeColor="#0000CC"
        onclick="btnPostback_Click" /> </td>
</tr>
</table>
</div>
</form>
</body>
</html>

Ø  Now open your postback2.aspx page and write this code :-

<html xmlns="http://www.w3.org/1999/xhtml">
 <head id="Head1" runat="server">
<title>Cross Page Postback Example in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <b><u>Welcome to postback2.aspx Page</u></b><br /><br />
<label id="lblName" runat="server" /><br /><br />
<label id="lblAddress" runat="server" />
</div>
</form>
</body>
</html>

Ø    Than write this code at postback2.aspx’s codebehind file(postback.aspx.cs):-
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
        {
            TextBox txtName = (TextBox)PreviousPage.FindControl("txtName");
            TextBox txtAddress = (TextBox)PreviousPage.FindControl("txtAddress");
            lblName.InnerText = "Welcome to postback2.aspx page " + txtName.Text;
            lblAddress.InnerText = "Your Address is: " +txtAddress.Text;
        }
        else
        {
            Response.Redirect("postback1.aspx");
        }

This is all about postback and crosspostback concept in asp.net……
Thanks all of my friends….

1 comment:

  1. Postback means the page posted back its self after operation.Thanks for your valuable information. dot net training in chennai | best dot net training in chennai

    ReplyDelete