Wednesday, 22 August 2012

Cross Page Posting Example

 Cross Page Posting

I have scenario that I want to transfer control value one page to another on button click event. Most of developer can achieve this functionality using Server.Transfer Method which preserver the HttpContext of current page before transferring to other page, because HttpContext is preserved you can access the source page’s items collection in target hence called cross page posting.
This functionality comes at price, the basic problem is that this transfer occurs at server level and current page must post back to itself before it can transfer to other page results extra processing overhead.
Now new version we can achieve this functionality using following examples.
I have two controls on one “Webform1.aspx”. “TxtName” as Textbox Control  and “BtnRedirect” as BtnControl. On Click of BtnRedirect I want transfer text box value into other form.  As per following screen shot I want transfer 32 value into “WebForm2.aspx”.


WebFormat1.aspx
When user clicks the button the current page will post back to the specified page which can access the source page controls through Page.PreviousPage property which returns the reference of previous page, once got the reference of previous page you can use the Find Control method to get the reference of particular.


WebForm2.aspx
PreviousPageType directive specifies the virtual path of the source page for strongly typed access to the source page, but the problem with this approach is that you can only specify one previous page type and it cannot be used for pages which can be destination for multiple pages in this case only option left is late bind access using Find Control method or reflection.
In this “Webform.aspx”  I declared Label and transfer value assign to this label.


No comments:

Post a Comment