Cross Page Posting in Asp.NET

In Simple words Cross page posting means allow a page to cross post / submit to another page ,

Using cross page posting we can access the control values from the previous page(source Page) i.e. from which it is get posted.

Although there are different methods like QueryString ,Server.Transfer ,Response.Redirect ,Session Objects,cookies are available for passing data between two pages,there are certain/specific cases such as filling multiple entry form in which we need to have the multiple entry data on the previous forms for some reference , in cases like mentioned we can specifically use Cross Page Posting,other mentioned methods are not so useful to use in these cases.

We can use the server controls ,which are implemented from IButtonControl interface e.g. Button ,LinkButton,ImageButton etc.

I am using one simple example to explain how to use ‘PostBackUrl’ property

Lets say we are creating one Car Insurance application and on the first page we are taking First name, Last name,Date & COuntry from the user and then we are forwarding to the user to next page for further filling out further details and creating the query using the filled data. In this case we can use.

PostBackUrl functionality following steps.

1) Set the the ‘Next’ button’s property ‘PostBackUrl’ to “~/PostBack2.aspx” or the page which is your next page. check the following sample page with postbackurl on ‘Next’ button.

PostBackUrl1

<asp:Button ID=”btnNext” runat=”server” PostBackUrl=”~/PostBack2.aspx” Text=”Next” Width=”97px” />

2)On the next page if you need to check whether the page was coming through cross-page posting or from other like Server.Transfer,Response.Redirect operation,The .Net provides new page property called IsCrossPagePostBack.

We need to check it like following

if (PreviousPage.Page.IsCrossPagePostBack)

{

}

3) We can get the values entered by user on previous page using FindControl(). In Our case it will look like as given below.

if (PreviousPage.Page.IsCrossPagePostBack)

{

TextBox tbxFirstName = (TextBox)PreviousPage.FindControl(“tbxFirstName”);

TextBox tbxLastName = (TextBox)PreviousPage.FindControl(“tbxLastName”);

TextBox tbxDate = (TextBox)PreviousPage.FindControl(“tbxDate”);

DropDownList ddlCountry = (DropDownList)PreviousPage.FindControl(“ddlCountry”);

lblMessage.Text = tbxFirstName.Text + ” ” + tbxLastName.Text;

} We can see values from First & second textbox retrieved on the second page as in below image.

PostBackUrl2

Note : While using Master pages,we need to find the previous page control at the 2 levels deep , First we need to find out ContentPlaceHolder of the masterpage and then we need to find the control from ContentPlaceHolder

e.g.

(TextBox)PreviousPage.Master. FindControl(“ContentPlaceHolder1”). FindControl(“tbxFirstName”);

Detail Ref – MSDN

CrossPagePosting Property Values

When page Default.aspx posts to page PostBack2.aspx then at PostBack2 ( @ Page Load) we can find following property values

  • this.Page.IsPostBack is false
  • this.Page.PreviousPage.IsPostBack is true
  • this.Page.IsCrossPagePostBack is false
  • this.Page.PreviousPage.IsCrossPagePostBack is true


& here this.Page.PreviousPage has a reference to page Default.aspx


Setting Width or Font size from code behind / dynamically

When we are declaring any Label/button from code behind then we can also set the font size/width for the same

For Width its considered in Unit & Font Size is considered in FontUnit

For Label

We can’t use directly –

lblParameterName.Font.Size = “10px”;                                                                                                                                                              you will get error – Cannot implicitly convert type ‘string’ to ‘System.Web.UI.WebControls.FontUnit’

We can use the following way –  lblParameterName.Font.Size = new FontUnit(10); Here we are setting the fontsize to 10;

For Button –

We can’t use directly –

btnParameterName.width = “10px”;     you will get error – Cannot implicitly convert type ‘string’ to ‘System.Web.UI.WebControls.Unit’

We can use the following way –  btnParameterName.width = Unit.Pixel(10); Here we are setting the widthto 10px;

Detail Ref – MSDN Unit