Saturday, February 27, 2010

IsCrossPagePostback–pass data from one page to another

Hello friends
again with new concept..

Page.IsCrossPagePostBack
in many case we are supposed to pass data from one page to another page.

So we can pass data through Querystring, ,
this options are quiet helpful when Data is less in quantity.. but now what if we want whole page to another page… ?

and in previous page Data is in very big amount around 20 – 40 fields…
so it is not feasible to pass that data in querystring or storing all data in session.

there also another ways, like Cookie or hiddenfields using Post method.. but
all this require more trouble..

but what if we get previous page controls like it is “on same page”

for that we have a good option called IsCrossPagePostback 

it is same like our Page.IsPostBack.
Page.IsPostBack treats only for current page’s postback
but to track weather postback is from Current page or previous page we can use IsCrossPagePostBack.

One Important Note : Respose. Redirect will not help us to track IsCrossPagePostBack
for that we must have Server.Transfer or PostbackURL of Button control


So here we have a sample of IsCrossPagePostBack

in First.aspx
   1: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="First.aspx.cs" Inherits="First" %>
   2:  
   3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4: <html xmlns="http://www.w3.org/1999/xhtml">
   5: <head runat="server">
   6:     <title>First Page</title>
   7: </head>
   8: <body>
   9:     <form id="form1" runat="server">
  10:     <div>
  11:         <asp:Label ID="label1" runat="server" Text="This is First Page"></asp:Label>
  12:         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  13:         <asp:Button ID="Button1" runat="server" Text="Pass To Second Page" PostBackUrl="~/Second.aspx" />
  14:     </div>
  15:     </form>
  16: </body>
  17: </html>










Now in Second.aspx




   1: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Second.aspx.cs" Inherits="Second" %>
   2:  
   3: <%@ PreviousPageType VirtualPath="~/First.aspx" %>
   4: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   5: <html xmlns="http://www.w3.org/1999/xhtml">
   6: <head runat="server">
   7:     <title>Second Page</title>
   8: </head>
   9: <body>
  10:     <form id="form1" runat="server">
  11:     <div>
  12:         <asp:Label ID="Label1" runat="server"></asp:Label>
  13:     </div>
  14:     </form>
  15: </body>
  16: </html>









in this page very important line is  line no 3


here the Reference is set to inherit controls of Previous page



   1: public partial class Second : System.Web.UI.Page
   2: {
   3:     protected void Page_Load(object sender, EventArgs e)
   4:     {
   5:         if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
   6:         {
   7:             TextBox text = PreviousPage.FindControl("TextBox1") as TextBox;
   8:  
   9:             if (text != null)
  10:             {
  11:                 Label1.Text = " This is from From First Page : " + text.Text;
  12:             }
  13:             else
  14:             {
  15:                 Label1.Text = "Empty !!!!";
  16:             }
  17:         }
  18:     }
  19: }









in page load it is very easy to fetch data of previous page..





so this is very simple if the pages are within same project..





we will see very soon when page is in another project means RemotecrossPage





All The Best

No comments: