Suggested Videos
Part 52 - Response.Redirect in asp.net
Part 53 - Server.Transfer in asp.net
Part 54 - Server.execute in asp.net
The following are the different page navigation techniques in asp.net
1. Hyperlink control - Discussed in Part 13 and Part 51 of the ASP.NET video series
2. Response.Redirect - Discussed in Part 52
3. Server.Transfer - Discussed in Part 53
4. Server.Execute - Discussed in Part 54
5. Cross-Page postback
6. Window.Open
In this video we will discuss about Cross page posting. Cross page posting allows to post one page to another page. By default, when you click a button, the webform posts to itself. If you want to post to another webform on a button click, set the PostBackUrl of the button, to the page that you want to post to.
WebForm1.aspx code: Notice that, the PostBackUrl property of the button with ID=btnCrossPagePostback is set to WebForm2.aspx. When this button is clicked WebForm1.aspx gets posted to WebForm2.aspx.
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>
This is WebForm1</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:TextBox ID="txtName" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:TextBox ID="txtEmail" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnCrossPagePostback" runat="server"
Text="Cross Page Postback - WebForm2"
Width="250px" PostBackUrl="~/WebForm2.aspx"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Server.Transfer - WebForm2" Width="250px" />
</td>
</tr>
</table>
</div>
WebForm1.aspx.cs code:
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("~/WebForm2.aspx");
}
WebForm2.aspx code:
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>This is WebForm2</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:Label ID="lblName" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:Label ID="lblEmail" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblStatus" runat="server"
ForeColor="Red" Font-Bold="true"></asp:Label>
</td>
</tr>
</table>
</div>
WebForm2.aspx.cs code: Page.IsCrossPagePostBack Property is used to indicate whether the page is involved in a cross-page postback.
protected void Page_Load(object sender, EventArgs e)
{
Page previousPage = Page.PreviousPage;
if (previousPage != null && previousPage.IsCrossPagePostBack)
{
lblName.Text = ((TextBox)previousPage.FindControl("txtName")).Text;
lblEmail.Text = ((TextBox)previousPage.FindControl("txtEmail")).Text;
}
else
{
lblStatus.Text = "Landed on this page using a technique other than cross page post back";
}
}
The problem with FindControl() method is that, if you mis-spell the ControlID, we could get a runtime NullRefernceException. In the next video we will discuss about obtaining a strongly typed reference to the previous page, which can avoid NullRefernceExceptions.
Part 52 - Response.Redirect in asp.net
Part 53 - Server.Transfer in asp.net
Part 54 - Server.execute in asp.net
The following are the different page navigation techniques in asp.net
1. Hyperlink control - Discussed in Part 13 and Part 51 of the ASP.NET video series
2. Response.Redirect - Discussed in Part 52
3. Server.Transfer - Discussed in Part 53
4. Server.Execute - Discussed in Part 54
5. Cross-Page postback
6. Window.Open
In this video we will discuss about Cross page posting. Cross page posting allows to post one page to another page. By default, when you click a button, the webform posts to itself. If you want to post to another webform on a button click, set the PostBackUrl of the button, to the page that you want to post to.
WebForm1.aspx code: Notice that, the PostBackUrl property of the button with ID=btnCrossPagePostback is set to WebForm2.aspx. When this button is clicked WebForm1.aspx gets posted to WebForm2.aspx.
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>
This is WebForm1</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:TextBox ID="txtName" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:TextBox ID="txtEmail" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnCrossPagePostback" runat="server"
Text="Cross Page Postback - WebForm2"
Width="250px" PostBackUrl="~/WebForm2.aspx"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Server.Transfer - WebForm2" Width="250px" />
</td>
</tr>
</table>
</div>
WebForm1.aspx.cs code:
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("~/WebForm2.aspx");
}
WebForm2.aspx code:
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>This is WebForm2</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:Label ID="lblName" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:Label ID="lblEmail" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblStatus" runat="server"
ForeColor="Red" Font-Bold="true"></asp:Label>
</td>
</tr>
</table>
</div>
WebForm2.aspx.cs code: Page.IsCrossPagePostBack Property is used to indicate whether the page is involved in a cross-page postback.
protected void Page_Load(object sender, EventArgs e)
{
Page previousPage = Page.PreviousPage;
if (previousPage != null && previousPage.IsCrossPagePostBack)
{
lblName.Text = ((TextBox)previousPage.FindControl("txtName")).Text;
lblEmail.Text = ((TextBox)previousPage.FindControl("txtEmail")).Text;
}
else
{
lblStatus.Text = "Landed on this page using a technique other than cross page post back";
}
}
The problem with FindControl() method is that, if you mis-spell the ControlID, we could get a runtime NullRefernceException. In the next video we will discuss about obtaining a strongly typed reference to the previous page, which can avoid NullRefernceExceptions.
hi venkat,
ReplyDelete1 question,the button which is clicked to crosspagepostback.would the control would come to the click event of that button after executing the postback request of webform 2.
pls reply
thanks
I'm unable to load previous form details
ReplyDeleteif you use Friendly URLs then you must comment following row in RouteConfig.cs file:
ReplyDeleteroutes.EnableFriendlyUrls(settings);
otherwise we get previousPage = null ()