Using the page navigation control in ASP.net 2.0

Source: Internet
Author: User
Asp.net| Control | Page almost every site, in order to facilitate users in the site for page navigation, the use of page navigation control. With the function of page navigation, users can easily jump between pages in a complex website. In the previous web programming, to write a good page navigation function, is not so easy, but also to use some skills. In ASP.net 2.0, in order to facilitate page navigation, a new called page navigation control SiteMapDataSource, which can also be bound to different other page controls, such as Treeview,menu, very flexible, So that the different forms of page navigation can be easily implemented, but also provides a run-time programming interface, can be programmed to dynamically implement the page navigation control. This article will simply take a few examples to illustrate how to implement page navigation in ASP.net 2.0.

Structure and SiteMapDataSource controls for page navigation

In ASP.net 2.0, to implement page navigation, you should first provide the page structure level of the entire website in the form of XML. We can write an XML text file called Web.sitemap that defines the hierarchy of the entire navigation page. Examples are as follows:

<?xml version= "1.0" encoding= "Utf-8"?
<siteMap>
<sitemapnode title= "Default" description= "Home" url= "default.aspx"
<sitemapnode title= "Members", description= "members" url= "Members.aspx"
<sitemapnode title= "My Account" description= "I Account" url= "myaccount.aspx"/>
<sitemapnode title= "Products" description= "Products" url= "products.aspx"
</siteMapNode>
<sitemapnode title= "Administration" description= "Administration" url= "~/admin/default.aspx"
<sitemapnode title= "Customer" description= "Customer Admin" url= "~/admin/customer/default.aspx"/>
<sitemapnode title= "Products admin" description= "Products admin" url= "~/admin/productsadmin.aspx"/>
</siteMapNode>
</siteMapNode>
</siteMap>
We can see that the Web.sitemap file must contain a root node sitemap. Moreover, set a parent SiteMapNode node, which indicates that the default site home, in the parent SiteMapNode node, you can have a number of SiteMapNode nodes, respectively, according to the hierarchy of the site of the various columns (note the above example, The inclusion relationship between each child node). In each SiteMapNode node, there are several attributes:

1 URL attribute: This attribute indicates the address link of the column to be navigated, in the definition of Web.sitemap, must be the relative address of each column.

2 Title attribute: This attribute indicates the name of each sub column, which is displayed on the page.

3 Description Property: When this property is specified, the user appears related to the column when the mouse moves to the column, similar to the ToolTips property.

After you have designed the Sitemap properties, you can then build the page navigation function step-by-step, with two main steps:

1 Add the SiteMapDataSource control to the page. The control automatically senses the content in the binding Web.sitemap.

2 Bind the SiteMapDataSource control to controls such as Sitemappath,treeview,menu, that is, set their data source to the SiteMapDataSource control.

Once we know the method, we will use Treeview,menu,sitemappath three controls as an example to explain how to work with the SiteMapDataSource control.

Let's first describe the methods used with the TreeView control and the SiteMapDataSource control. The TreeView Tree List control is ideal for page navigation, in order to specify that we take full advantage of the masterpage controls in ASP.net to build a basic framework for a Web site.

In the Visual Web Developer 1, create a new Web site, add the Web.sitemap file above, and add a master-type page called Navigation with the following code:

<%@ Master language= "C #"%>


<title> Master Page </title>

<body>
<form id= "Form1" runat= "Server"
<div>
<table style= "width:100%; height:100% "border=" 1 ">
<tr>
<TD style= "width:10%"
<asp:treeview id= "TreeView1" runat= "Server" datasourceid= "SiteMapDataSource1"
Expanddepth= "2" showexpandcollapse= "False" nodeindent= "10" >
<LevelStyles>
<asp:treenodestyle font-bold= "True" font-underline= "False"/>
<asp:treenodestyle font-italic= "True" font-underline= "False"/>
<asp:treenodestyle font-size= "X-small" imageurl= "Bullet.gif" font-underline= "False"/>
</LevelStyles>
<nodestyle childnodespadding= "10"/>
</asp:TreeView>
</td>
<TD style= "width:100px"
<asp:contentplaceholder id= "ContentPlaceHolder1" runat= "Server"
</asp:contentplaceholder>
</td>
</tr>
</table>
<asp:sitemapdatasource id= "SiteMapDataSource1" runat= "Server"/>
</div>
</form>
</body>

In the above code, the SiteMapDataSource control is specified in the Datasoruce property in the TreeView control, and the style of the different nodes is also defined in the TreeView control.

After completing the MasterPage page, it is equal to already set up the template page of the website, then you can create additional subpages to inherit the MasterPage page, and create the contents of the respective pages. For example, create a new Default.aspx page with the following code:

<%@ page language= "C #" masterpagefile= "Navigation.master" title= "Default Page"%>
<asp:content contentplaceholderid= "ContentPlaceHolder1"
Id= "Content1" runat= "Server"
This is the default page
</asp:Content>
As you can see, when you create a template page, you can create additional subpages, just write different content in the ContentPlaceHolderID. After running, the effect is as shown:

  


Next, let's describe how to work with the menu control and the SiteMapDataSource control. Which we have on the basis of the example above, in <table style= "width:100%; height:100% "border=" 1 "> add the following code to it,

<TR height= "100px" >
<TD colspan= "2" align= "left"
<asp:menu id= "Menu1" runat= "Server"
Datasourceid= "SiteMapDataSource1" >
</asp:Menu>
</td>
</tr>
Among them, we added a menu control, which set its DataSourceID property to SiteMapDataSource1, and run the following figure, of course, we can change the menu control display position, if it can be changed to vertical style display.


And for the navigation bar that we often see showing the current path of the page, it can be easily implemented in ASP.net 2.0, where we can use the SiteMapPath control. We then add the following code under the Menu control in the previous code:

<TR height= "100px" >
<TD colspan= "2" align= "left"
Currently Selected Page is:
<asp:sitemappath runat= "Server" id= "SiteMapPath1" > </asp:SiteMapPath>
</td>
</tr>
Note that just adding the SiteMapPath control is OK because it is automatically bound to the SiteMapDataSource control that has been added. In order to illustrate the problem, add another page member.aspx, the code is as follows:

<%@ page language= "C #" masterpagefile= "Navigation.master" title= "members Page"%>
<asp:content contentplaceholderid= "ContentPlaceHolder1" id= "Content1" runat= "Server"
This are the Members page
</asp:Content>
The results of the operation are as follows:

    

Finally, let's take a look at how to programmatically get the relevant data in the page navigation. Among them, you must refer to the Sitemap class, which provides a number of related methods and properties, most importantly the CurrentNode attribute, which can indicate the current user is browsing which column page, which is used to track the user's action trajectory in the site, and the site data statistics, Sometimes it is useful, for example, as follows:

<%@ page language= "C #" masterpagefile= "Navigation.master" title= "members Page"%>
<script runat= "Server" >
void Page_Load (object sender, EventArgs e)
{
Response.Write ("The currently selected root node is:" + SiteMap.CurrentNode.Description + "<br>");
Response.Write ("The Parent for the currently selected node is:" +
SiteMap.CurrentNode.ParentNode.Description);
}
</script>
<asp:content contentplaceholderid= "ContentPlaceHolder1" id= "Content1" runat= "Server"
This are the Members page
</asp:Content>
In this example, the way to use the program, the user is currently browsing the column page, and the column of the name of the parent column, the results of the operation of the following figure:


It can be seen that in ASP.net 2.0, the function of the page navigation is very flexible and convenient, the function is very powerful, more functions please refer to MSDN.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.