PM's Blog

Pramod Mohanan's notes about ASP.NET, MVC, C#, SQL, jQuery, Bootstrap

Cross Page PostBack in ASP.net

A webform by default posts back to itself. An Asp.net page can be made to post back to another page by using the PostBackUrl attribute of the ASP.Net button control.

In the below example we access the Text attribute of a ASP.NET Button of a page(first.aspx) in another page(second.aspx)

in first.aspx

<asp:Button ID="btnCrossPostBack" runat="server" PostBackUrl="~/second.aspx"
Text="Cross Page Post Back" />

in second.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
   if (PreviousPage != null)
   {
      Button btn = (Button)PreviousPage.FindControl("btnCrossPostBack");
      Response.Write("The text of the button in first.aspx was : " + btn.Text);
    }
}

When the button is clicked the page second.aspx would show up as

The text of the button in first.aspx was : Cross Page Post Back

Additionaly, if the following page directive is added to second.aspx the class first.aspx is dynamically inherited by second.aspx thereby allowing public properties of first.aspx to be accessed in second.aspx

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

Leave a Reply

Your email address will not be published. Required fields are marked *

PM's Blog © 2018