Saturday, August 30, 2008

Cross-Page Posting for handling some combination of user input

I believe everybody know ASP .Net supports same page Postback function by default. For example, same page posting back to the same page. For those that do not know, ASP .Net 2.0 introduces a new capability whereby pages can post to pages other than themselves. We call this Cross-Page posting and usually it gives a tight coupling for these pages.

Now, let's see what we need to create here.

Let's use Page 1 as a Posting Page and Page 2 as the Receiving Page.

  1. To create a Cross Page posting, we can use PostbackURL from the button (LinkButton, Button, ImageButton) in Page 1 and point it to Page 2.
  2. <asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/Page 2.aspx" />



  3. In Page 2, add a PreviousPageType directive. This will allow you know set your previous page to Page 1.


  4. <%@ PreviousPageType VirtualPath="~/Page 1.aspx" %>




Or Use TypeName to Class name



<%@ PreviousPageType TypeName="_Page_1" %>



Above steps will let you set the tight coupling relationship between Page 1 and Page 2 and you can access the properties, control or functions in Page 1 from Page 2.



public partial class Page_2 : System.Web.UI.Page
{
_Page_1 PostingPage;

protected void Page_Load(object sender, EventArgs e)
{
if (this.PreviousPage != null)
{
PostingPage = this.PreviousPage;

Label1.Text = PostingPage.Page1_Propertise;
}
else
{
Label1.Text = "Previous Page is Null";
}
}
}



Usually you can use when creating wizard or pages that required tight coupling. You have to set PreviousPageType directive in order to access Page 1 properties.



Okie now, you might ask how to access a page's properties, control, or function without using PreviousPageType directive. The answer is..you may use Reference directive. Let's create the scenario now.



Now, we do not want to use PostbackURL at the button in Page 1, instead, we use a normal navigation method.



<asp:Button ID="Button1" runat="server" Text="Button"OnClick="Button1_Click" />


In the Page 1 code behind,



protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("~/Page 2.aspx");
}



Now, we need to add reference in Page 2 so that it can access Page 1 with no problem.



<%@ Reference Page="~/_Page_1.aspx" %>


By using reference page, you may need to cast your page object like the code below.



public partial class Page_2 : System.Web.UI.Page
{
_Page_1 PostingPage;

protected void Page_Load(object sender, EventArgs e)
{
if (this.PreviousPage != null)
{
PostingPage = (_Page_1)this.PreviousPage;

Label1.Text = ((Label)PostingPage.FindControl("Label2")).Text;
}
else
{
Label1.Text = "Previous Page is Null";
}
}
}


Now, Questions raised when come to validation and Cross Page posting. If client-side validation (using javascript) is disabled on a web form, the validation will greatly depend on server-side validation. But using PostbackUrl on button will redirect to a new page. Although the server side validation has been in place, the problem is being ignored.



What you can do here is to use PreviousPage.IsValid() function.



See the samples below:



protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
if (PreviousPage.IsValid == true )
{
//Continue
}
else
{
//Return to previous page
}
}
}

Hope the content helps. Cross Page posting may come handy when you really need a tight coupling pages like Wizards.



1 comment:

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!