Tuesday, December 27, 2011

CrossPagePostBack in ASP.NET and C#


In This Tutorial we are performing CrossPagePostBack which submits one page(Page1-default.aspx) controls to another page( Page2-CrossPagePostBack.aspx) and use that first page values in that another page.

When we works with controls like buttons on page and submit them, by default, they submit on the same page that is postback. In some conditions you want to postback page to anotherpage that's why we provide PostBackUrl as another page submission.

When you configure a page for cross-page posting, you frequently want to get information from the source page. This might include the information from controls on the page—that is, the information being posted by the browser—as well as public properties of the source page.

The Page class exposes a property named PreviousPage. If the source page and target page are in the same ASP.NET application, the PreviousPage property in the target page contains a reference to the source page. (If the page is not the target of a cross-page posting, or if the pages are in different applications, the PreviousPage property is not initialized.) By default, the PreviousPage property is typed as Page.  

Step 1:  File->New->Website

Step 2:
page1 code
default.aspx page code split view
default.aspx.cs page code

Step 3:  
page2 code
CrossPagePostBack.aspx  code split view :

CrossPagePostBack.aspx.cs page code


View in Browser:

Here we are submitting page on same page and on another page by clicking "Submit on same page" button and "Submit on other page" button respectively.

No comments:

Popular Posts