Tuesday, December 15, 2009

Cross Page Posting or Postback in Asp.net 2.0 / 3.5




Please visit my new Web Site WWW.Codedisplay.com



In Asp.net 1.1 we can postback a form itself but can't posting back to other pages. This is a limitation of Asp.net 1.0. We can simulate cross page postback by using Response.Redirect or Server. Transfer that i have already describedin one of my previous post named "Passing data/parameters/values from one aspx page to another aspx page".

In Asp.net 2.0 or Asp.net 3.5 microsift gives us a solution to easily do the cross page postback that i will cover in this article. The feature known as "Cross Page PostBack" from a web form or aspx page to post back to a different or another web formor aspx page.

Asp.net 2.0 or 3.5 offers us to post back to another form by using PostBackURL property of a control that implements IButtonControl. Here note that Button, ImageButton, LinkButton has the PostBackURL property to submit one page/form to different page/form. Also provides us a FindControl Method to catch the controls of previous page or source page. By using PreviousPage property we will catch the page that has been submitted. So using PreviousPage property to catch the previous page & FindControl Method to catch the controls of this previous page.

There are two ways we can post back a form into another form. These are:
1. Direct access controls from target page
2. Direct access public properties from target page

Direct access controls from target page:
To describe this example first create a project. Then rename the default.aspx page to page1.aspx. Add one another page & rename it by page2.aspx. Here we will generate cross page postback from page1.aspx to pag2.aspx page. Now add two TextBox in source page means page1.aspx and a Button control. Set the PostBackUrl="page2.aspx" of theButton control. No server side code is required for the page1.aspx since after clicking on the page1, Button Control will submit itsself into the page2.aspx that i have specified in PostBackUrl property. Have a look at source page page1.aspx markup:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="page1.aspx.cs" Inherits="page1" %>

<!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>Cross page postback example 1</title>
</head>
<body>
<form id="form1" runat="server">

<div>
<asp:Label ID="lblFirstNum" runat="server">Enter First Number:</asp:Label>
<asp:TextBox ID="txtFirstNum" runat="server"></asp:TextBox>

<br />

<asp:Label ID="lbl2ndNum" runat="server">Enter 2nd Number: </asp:Label>
<asp:TextBox ID="txt2ndNum" runat="server"></asp:TextBox>

<br /><br />

<asp:Button ID="cmdCross" runat="server" Text="Click to Trigger Postback" PostBackUrl="page2.aspx" />
</div>

</form>
</body>
</html>

Now we have to do some work on page2.aspx to collect data from page1.aspx. By using PreviousPage property & FindControl Method we can easily catch data from source page. Have a look at the page2.aspx page_load event where we catch source page first number & Second number & show the cumulative result in page2.aspx page.
Code behind of page2.aspx:


using System;

using System.Web.UI.WebControls;

public partial class page2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if(!IsPostBack)

{

if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)

{

decimal nFirstNum = Convert.ToDecimal(((TextBox)PreviousPage.FindControl("txtFirstNum")).Text);

decimal n2ndNum = Convert.ToDecimal(((TextBox)PreviousPage.FindControl("txt2ndNum")).Text);

Response.Write("Sum of previous page two number is: " + (nFirstNum + n2ndNum));

}

else

{

// This is not cross page postback.

// Implement Other logic here

}

}

}

}

If you use master page then you need to give diefferent treatment to get data from cross page postback. Keep page1.aspx as it is. Only modify the page2.aspx page_load event in the following way:


using System;

using System.Web.UI.WebControls;



public partial class mPage2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)

{

ContentPlaceHolder content = (ContentPlaceHolder)Page.PreviousPage.Form.FindControl("ContentPlaceHolder1");

decimal nFirstNum =Convert.ToDecimal(((TextBox)content.FindControl("txtFirstNum")).Text);

decimal n2ndNum = Convert.ToDecimal(((TextBox)content.FindControl("txt2ndNum")).Text);

Response.Write("Sum of previous page two number is: " + (nFirstNum + n2ndNum));

}

else

{

// This is not cross page postback.

// Implement Other logic here

}

}

}

}



Direct access public properties from target page:
To direct access public properties you have to create some public properties in source page which we will access from target page. To do that modify page1.aspx page server side code in the following way & keep HTML mark up as it is:


using System;

public partial class page1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

public decimal nFirstNum
{
get { return Convert.ToDecimal(txtFirstNum.Text); }
}

public decimal n2ndNum
{
get { return Convert.ToDecimal(txt2ndNum.Text); }
}
}

Don't forget to add the below page directives into the page2.aspx. Otherwise the next code segment will give you error since in page_load event you will acess page1 public properties. The directive is:

<%@ PreviousPageType VirtualPath="~/page1.aspx" %>

Now also modify page2.aspx page_load event in the following way:


using System;

using System.Web.UI.WebControls;


public partial class page2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if(!IsPostBack)

{

if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)

{

decimal nFirstNum = PreviousPage.nFirstNum;

decimal n2ndNum = PreviousPage.n2ndNum;

Response.Write("Sum of previous page two number is: " + (nFirstNum + n2ndNum));

}

else

{

// This is not cross page postback.

// Implement Other logic here

}

}

}

}


The Output will be like this:

Fig: Output

Note:
If your source page has more than one control that implements IButtonControl than you can define different target page fordifferent controls.

In this example i showed you how to make Cross Page Posting or postback in ASP.NET 2.0 / 3.5 using C#. Hope it will help.

3 comments:

Bijayani said...

Hi Shawpnendu,

I happened to see your post and find it quite impressive and informative. I would like to share a link where a software engineer has shared a tip on "Post Data from One form to other form using Cross-Page techiniuqe in ASP.NET 2.0".

Here is the link:
http://www.mindfiresolutions.com/post-data-from-one-form-to-other-form-using-crosspage-techiniuqe-in-aspnet-20-767.php

Hope you find it useful and of assistance.

Anonymous said...

I have used the simillar code but my pages are inherited from Master page and so its giving me empty output, Is there any idea. My code for target page is is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Members_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
ContentPlaceHolder cp = PreviousPage.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
if (cp != null)
{
TextBox tb = cp.FindControl("TextBox1") as TextBox;
if (tb != null)
{
Pre_meetingid.Text = tb.Text;
}
}
}
}
}

and Sender page is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Members_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int chid;
chid = Convert.ToInt32(DetailsView1.Rows[0].Cells[1].Text);
TextBox1.Text = chid.ToString();
}

It shows me value on Form1 in textbox but it doesnt transfer to second form.

Anonymous said...

Please refer this link for master page

http://www.dotnetcurry.com/ShowArticle.aspx?ID=71

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash