Wednesday 24 September 2014

What is CrossPagePostBack Concept in Asp.net

What   is CrossPagePostBack   Concept in Asp.net.........................

The following are the different page navigation techniques in asp.net
·         HyperLink Control
·         Response.Redirect
·         Server.Transfer
·         Server.Execute
·         Cross Page PostBack
·         Window.Open

Cross Page PostBack :  it is allows to post page to another page by default when you click a button, the Web Form post to itself if you want  to post to another Web Form  on  a button click set the PostBackUrl of button to the page that you want to post to

Page.IsCrossPagePostBack  this property is used to indicate when there the page is involved in cross-page-postback

For Example 

Source code with Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
<style type="text/css">
.style1 {
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td>Name</td>
<td><asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>EmailId</td>
<td><asp:TextBox ID="txtemailid" runat="server"></asp:TextBox></td>
</tr>
<tr><td>&nbsp;</td>
<td>
<asp:Button ID="btnSave" runat="server"
Text="Cross Page PostBack"
PostBackUrl="~/Default2.aspx" onclick="btnSave_Click"  />
&nbsp;
<asp:Button ID="btnServerTransfer" runat="server" Text="Server.Transfer"
onclick="btnServerTransfer_Click" /></td>
</tr>
</table>

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


Source code with Default.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

}
protected void btnServerTransfer_Click(object sender, EventArgs e)
{
Server.Transfer("Default2.aspx");
}
protected void btnSave_Click(object sender, EventArgs e)
{

}
}

Source code with Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table class="style1">
<tr>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
Name</td>
<td>
<asp:Label ID="lblname" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
EmailId</td>
<td>
<asp:Label ID="lblemailid" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2"><center><asp:Label ID="lblstatus" runat="server" Visible="false" Text="Label"></asp:Label></center>

</td>
</tr>
</table>

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


Source code with Default2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page previousePage = Page.PreviousPage;
if (previousePage != null && previousePage.IsCrossPagePostBack)
{
lblname.Text = ((TextBox)previousePage.FindControl("txtname")).Text;
lblemailid.Text = ((TextBox)previousePage.FindControl("txtemailid")).Text;
}
else
{
lblstatus.Visible=true;
lblstatus.ForeColor=Color.Red;
lblstatus.Text = "you landed on this page using"+"</br>"+"  a technique other than cross page post back";

}
}
}

Result
        



2 comments:

  1. Greatly appreciating blog as usual and this time with new trick that is the good thing. There are the solution of all major problem which comes in the mid phase or beginning phase of .net developing.

    ReplyDelete