Asp.net site navigation tutorial


Asp.net site navigation tutorial - contributed by Nihal Singh

.NET Tutorial > Site navigation

Site navigation

Page navigation is the process of moving from one page to another page in your website. There are many ways to navigate from one page to another in ASP.NET.

1. Client-side navigation
2. Cross-page posting
3. Client-side browser redirect
4. Server-side transfer

1. Client-side navigation

The HyperLink control is used to create a link to another Web page. The Text property is used for displaying the text in the HyperLink. We can also display a image on this control instead of text. To display an image we should set the ImageUrl property.

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Welcome.aspx">Goto Welcome Page</asp:HyperLink>

You can also use JavaScript for client side navigation. You should use HTML input button for this.

Example:

function Button1_onclick()
{
              document.location="WelcomePage.aspx";
}

2. Cross-page posting

By default, buttons and other controls that cause a postback on an ASP.NET Web page submit the page back to itself. ASP.Net provides a feature known as Cross Page PostBack that enables a web form to post-back to a different web form. The Page class has 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.

Example:

We have two web pages named Home.aspx and Welcome.aspx respectively. The first page contains a Button and a Textbox. Button control that has its PostBackUrl property set to “~/Welcome.aspx”

// Sample code for Welcome.aspx.

if (Page.PreviousPage != null)
{
              TextBox MyTextBox = (TextBox)Page.PreviousPage.FindControl("TextBox1");
              if (MyTextBox!= null)
              {
                            Label1.Text = MyTextBox.Text;
              }
}

3. Client-side browser redirect

The Page. Response object has redirect method. The Redirect method 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.

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

4 Server-side transfer

The Transfer method transfers all the state information in one ASP .NET Page to a second ASP.NET Page. Server.Transfer changes the page being rendered on the browser. This happens all on the server. A redirect is not issued to the web browser.

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



Write your comment - Share Knowledge and Experience



 
Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring