Site Navigation in ASP.NET 2.0: In web application when moving from one web page to another it is required to collect data from user.
Methods to Navigate Pages: 1.Provide Client side code or markup to request a new web page 2.Cross page posting 3.Issue client side browser redirect 4.Issue server side transfer
PROVIDING CLIENT SIDE CODE OR MARKUP TO REQUEST A NEW WEB PAGE: One of the easiest ways to navigate to a different web page is to provide a HyperLink control on the form and set the NavgateUrl property to the desired destination.
HyperLink control generates an <a> element in the HTML and the NaviagateUrl property is placed into the href attribute of the <a> element.
Ex: <asp:HyperLink ID=”hlNavigation” runat=”server” NaviagateUrl=~/Test.aspx”>Navigate to Test Page</asp:HyperLink>
HTML will be [CODE} <a ID=”hlNavigation href”Test.aspx”>Naviagate to Test page</a>
If this control is placed on a web page Home.aspx , and the HyperLink control is clicked browser will simply requests the Test.aspx page without no data is posted to Test.aspx.
Client side code can also perform web page navigation by changing the documents object’s location property to a new URL. The document object is the object that represents the web page, setting its location property causes the browser to request the web page at the new URL.
Ex: <input type=”button” id=”btnNavigate” value=”Naviagate to Test Page” onClick=”return btnNavigate_onclick()” />
<script language="javascript" type="text/javascript"> function btnNavigate_onclick() { ocument.location="Test.aspx"; } </script>
This code also navigate to Test.aspx with no data posted to Test.aspx page. To post data we have to find another way.
CROSS PAGE POSTING:
Cross-page posting is frequently desired in a scenario where data is collected on one Web page and processed on another Web page that displays the results. a Button control has its PostBackUrl property set to the Web page to post back to . In the processing page, which is the Web page that you post back to, the data from the first Web page is available.
The processing page collects data from previous page by using PreviousPage property of page object. The PreviousPage property is set if we are cross page posting and if it is set ot Nothing no cross page posting occurs.Any controls in the previous page can be accessed by using the FindControl() Method.
ACCESSING STRONGLY TYPED DATA:
Another way to access previous page data is to create public properties that expose the data that you need to access. We need to set the PreviousPageType directive on the result page.
Ex: the Home .aspx page performs a cross-page PostBack to Test.aspx using a public property and the PreviousPageType directive. The Home.aspx contains a TextBox control named txtData and a Button control on which the PostBackUrl property has been set to ~/Test.aspx.
public string PageData { get { return txtData.Text; }}
<%@ PreviousPageType VirtualPath="~/Home.aspx" %>
protected void Page_Load(object sender, EventArgs e) { if (PreviousPage == null) { lblData.Text = "No PreviousPage"; } Else { lblData.Text = PreviousPage.PageData; } }
ISSUING CLIENT SIDE BROWSER REDIRECT:
The Response object has a method called Redirect that you can use in your server-side code to instruct the browser to request a different page. The server-side code can process the PostBack and then execute the Redirect, where the Home.aspx page contains a Button control that performs a redirect to the Test.aspx page when the Button is clicked:
protected void Button1_Click(object sender, EventArgs e) { Response.BufferOutput = true; //process data Response.Redirect("Test.aspx"); } {/CODE]
BufferOutput must be set to true in order to perform the redirect to ensure that no data is sent to the browser prior to executing the Redirect method. If data is sent to the browser prior to executing the Redirect method, an HttpException is thrown, indicating that you cannot redirect after the Hypertext Transfer Protocol (HTTP) headers are sent.
The PreviousPage property does not get populated when using the Redirect method. To access data from the original page, you need to resort to traditional methods of passing data, such as placing the data into cookies, session state variables, or passing the data in the QueryString.
ISSUING SERVER SIDE TRANSFER: you can switch control to a different Web page by using the Transfer method on the HttpUtility object .An instance of the HttpUtility class is stored on the Page object, in the Server property. The server-side code can process the PostBack and then execute the Transfer method.
protected void Button1_Click(object sender, EventArgs e) { Server.Transfer("Test.aspx", false); }
The Transfer method accepts a Boolean parameter called preserveForm that you set to indicate your desire to keep the form and QueryString data. It is generally better to set this to false. You can also access the PreviousPage property to pass data between pages, just as you do when cross-page posting.
|
| Author: Mahesh Raj 07 Jun 2008 | Member Level: Gold Points : 1 |
This is very good information,Continue posting such useful articles.
|
| Author: John Fernandez 08 Jun 2008 | Member Level: Gold Points : 1 |
Very well written Article.Thanks for sharing this information.
|