Do you know that? 25% freelancers are 1-2 years as a freeleancer next

What are the different method of navigation in ASP.NET?

Page navigation means moving from one page to another page in your web site and another. There are many ways to navigate from one page to another in ASP.NET.

- Client-side navigation
- Cross-page posting
- Client-side browser redirect
- Client-Side Navigation

Client-side navigation:

Client-side navigation allows the user to navigate from one page to another by using client side code or HTML. It requests a new Web page in response to a client-side event, such as clicking a hyperlink or executing JavaScript as part of a button click.

Example:

Drag a HyperLink control on the form and set the NavigateUrl property to the desired destination page.

HyperLinkControl: Source

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Welcome.aspx"> Take a test from CareerRide </asp:HyperLink>

Suppose that, this control is placed on a Web page called CareerRide.aspx, and the HyperLink control is clicked, the browser simply requests the Welcome.aspx page.

Second method of client-side navigation is through JavaScript.

Example:

Take an HTML button control on web page. Following is the HTML code for the input button.

<input id="Button1" type="button" value="Go to next page" onclick="return Button1_onclick()" />

When the Button1 is clicked, the client-side method, Button1_onclick will be called. The JavaScript source for the Button1_onclick method is as follows:

<script language="javascript" type="text/javascript">
function Button1_onclick()
{
document.location="NavigateTest2.aspx";
}

</script>

Cross-page posting:

Example:

Suppose that we have two pages, the first page is FirstPage.aspx and Second page is SecondPage.aspx. The First Page has a Button and TextBox control and its ID is Button1 and TextBox1 respectively. A Button control has its PostBackUrl property. Set this property to ''~/SecondPage.aspx''. When the user clicks on Button, the data will send to SecondPage for processing. The code for SecondPage is as follows:

protected void Page_Load(object sender, EventArgs e)
{
if(Page.PreviousPage == null)
{
Label1.Text = "No previous page in post";
}
else
{
Label1.Text = ((TextBox)PreviousPage.FindControl("TextBox1")).Text;
}
}

The second page contains a Label control and its ID is Label1.

The page that receives the PostBack receives the posted data from the firstpage for processing. We can consider this page as the processing page.The processing page often needs to access data that was contained inside the initial page that collected the data and delivered the PostBack. The previous page's data is available inside thePage.PreviousPageproperty. This property is only set if a cross-page post occurs.

Client-side browser redirect:

ThePage.Responseobject contains theRedirectmethod that can be used in your server-side code to instruct the browser to initiate a request for another Web page. The redirect is not a PostBack. It is similar to the user clicking a hyperlink on a Web page.

Example:

protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect ("Welcome.aspx");
}

In client-side browser redirect method an extra round trip to the server is happened.

Server-side transfer:

In this techniqueServer.Transfermethod is used. TheTransfermethod transfers the entire context of a Web page over to another page. The page that receives the transfer generates the response back to the user's browser. In this mechanism the user's Internet address in his browser does not show the result of the transfer. The user's address bar still reflects the name of the originally requested page.

protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("MyPage.aspx", false);
}

The Transfer method has an overload that accepts a Boolean parameter called preserve-Form. You set this parameter to indicate if you want to keep the form and query string data.