Introduction:
In article How to Access a function from different Webpages i explained how to access a function from
another webpage under same project/namespace.In this article,we are going to discuss about the cross page postback in ASP.NET 2.0/3.5
Main:
what is cross page postback?,
cross page postback is simply to submit a form on one page (page1.aspx) and retrieve values of controls
of this page on another page (Page2.aspx).
See this below simple ex,
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="Firstlabel" runat=server/>
<asp:Button ID="Submit" runat="server"
OnClick="Submit_Click"
PostBackUrl="~/Page2.aspx"
Text="Cross page postback" /><br />
</div>
</form>
</body>
</html>
<!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>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label id="Firstlabel" runat=server/> <asp:Button ID="Submit" runat="server" OnClick="Submit_Click" PostBackUrl="~/Page2.aspx" Text="Cross page postback" /><br /> </div> </form> </body> </html> |
In Code Behind,
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
Label local_lbl;
local_lbl.Text = (Label)PreviousPage.FindControl("userid").Text;
}
}
protected void Page_Load(object sender, EventArgs e) { if (PreviousPage != null && PreviousPage.IsCrossPagePostBack) { Label local_lbl; local_lbl.Text = (Label)PreviousPage.FindControl("userid").Text; } } |
Conclusion:
Hope this helps,
Happy Coding.