Use sitemapdatasource page navigation control in ASP. NET 2.0

Source: Internet
Author: User
In ASP. NET 2.0UsePage navigation control

Used in ASP. NET 2.0SitemapdatasourcePage navigation control
In almost every website, in order to make it easier for users to navigate pages on the website, page navigation controls are indispensable. With the page navigation function, you can easily jump between pages on a complex website. In previous web programming, it was not so easy to write a good page navigation function. You also needed some skills. In Asp.net 2.0, a page navigation control calledSitemapdatasource,SitemapdatasourceYou can also bind different page controls, such as Treeview and menu, to make it easy to implement different forms of page navigation, it also provides runtime programming interfaces to dynamically implement page navigation controls in the form of programming. This article will briefly introduce several examples in Asp.net 2.0.SitemapdatasourceHow to Implement page navigation.
  Structure of page navigation and sitemapdatasource Control
In Asp.net 2.0, to implement page navigation, the page structure hierarchy of the entire website should be provided in XML format. We can compile an XML text file called Web. sitemap, which defines the structural hierarchy of the entire page to be navigated. Example: program code: <? 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 = "my 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 the root node sitemap. In addition, set a parent sitemapnode node, which indicates that it is the default homepage of the site. Under the parent sitemapnode node, there can be several subsitemapnode nodes, the sub-sections of the website are represented by hierarchies (note the inclusion relationship between the sub-nodes in the previous example ). Each sitemapnode has the following attributes:
1) URL attribute: This attribute specifies the address link of the topic to be navigated. In web. sitemap definition, it must be the relative address of each topic.
2) Title attribute: This attribute specifies the name of each subtopic, which is displayed on the page.
3) description attribute: This attribute is scheduled. When you move the cursor to this topic, a prompt is displayed for this topic, similar to the tooltips attribute.
After the sitemap attribute is designed, you can build the page navigation function step by step. There are two main steps:
1) Add to the pageSitemapdatasourceControl. This control automatically binds content in Web. sitemap.
2) SetSitemapdatasourceControls are bound to controls such as sitemappath, Treeview, and menu.SitemapdatasourceControl.
After knowing the method, we will take the Treeview, menu, and sitemappath controls as examples to illustrate howSitemapdatasourceControls.
The following describes how to use the Treeview control andSitemapdatasourceControls. The Treeview tree-like list control is very suitable for page navigation. To provide details, we fully utilize the masterpage control in Asp.net to build a basic framework architecture of a website.
In visual web developer 2005 Beta 1, create a website and add the preceding web. in the sitemap file, add a page named "navigation master". The Code is as follows: [blocked Advertisement] program code <% @ master language = "C #" %>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> master page </title>
</Head>
<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" performanceid ="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:SitemapdatasourceId ="Sitemapdatasource1 "runat =" server "/>
</Div>
</Form>
</Body>
</Html>

In the code above, the datasoruce attribute in the Treeview control specifiesSitemapdatasourceControls, and in the Treeview control, also defines the style of different nodes.
After the masterpage page is completed, the template page of the website has been created. Next, you can create other subpages to inherit the masterpage page and create the content of the respective pages. For example, create a new default. on the ASPX page, the Code is as follows: [blocked ads] program 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 a template page is created, you can create other subpages. You only need to write different content in the contentplaceholderid. Effect after running

Next, we will introduce how to set the menu control andSitemapdatasourceControls. Based on the preceding example, add the following code in <Table Style = "width: 100%; Height: 100%" border = "1">, [blocked Advertisement] program code <tr Height = "100px">
<TD colspan = "2" align = "Left">
<Asp: menu id = "menu1" runat = "server"
Performanceid ="Sitemapdatasource1 ">
</ASP: menu>
</TD>
</Tr>

A menu control is added, in which the performanceid attribute is setSitemapdatasource1. Run the command. Of course, you can change the display position of the menu control. For example, you can change it to a vertical display.

The navigation bar function that we often see shows the current path of the page can also be easily implemented in Asp.net 2.0, where we can use the sitemappath control. Next, under the Menu Control in the above Code, add the following code: program 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 you only need to add the sitemappath control, because it automatically matchesSitemapdatasourceControl. To illustrate the problem, we add another member page. aspx, code: [blocked Advertisement] program code <% @ page Language = "C #" masterpagefile = "navigation. master "Title =" members page "%>
<Asp: Content contentplaceholderid = "contentplaceholder1" id = "content1" runat = "server">
This is the members page
</ASP: content>

The running result is as follows:
    

Finally, let's take a look at how to obtain the relevant data in the page navigation through programming. The sitemap class must be referenced. This class provides many related methods and attributes, and the most important is the currentnode attribute, it can point out which topic page the current user is browsing, which is used to track the user's action track on the website and conduct site data statistics. Sometimes it is very useful, for example: [blocked Advertisement] program code <% @ 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 is the members page
</ASP: content>

In this example, a program is used to obtain the current topic page that the user is browsing and the name of the parent topic of the topic. The running result is as follows:

It can be seen that in Asp.net 2.0, the page navigation function is flexible and convenient, and the function is very powerful. For more functions, see msdn.
SitemapdatasourceClass
Note: This class is added in. NET Framework 2.0.

A data source control is provided. Web server controls and other controls can be used to bind the control to hierarchical site map data.
SitemapdatasourceA widget is a data source for site map data. site data is stored by the site map provider configured for the site.SitemapdatasourceEnables Web server controls (such as Treeview, menu, and dropdownlist controls) that are not specifically used as site navigation controls to bind to hierarchical site map data. You can use these Web server controls to display a site map as a directory or actively navigate the site. You can also use the sitemappath control, which is specially designed as a site navigation control.SitemapdatasourceControl instance.

SitemapdatasourceBind the map data to the site and display its view based on the Start Node specified in the site map hierarchy. By default, the Start Node is the root node of the hierarchy, but it can also be any other node in the hierarchy. The Start Node consists of the followingSitemapdatasourceAttribute Value to identify:

Start Node
Attribute Value

The root node of the hierarchy (default ).
Startfromcurrentnode is false.

Startingnodeurl is not set.

The node of the page currently being viewed.
Startfromcurrentnode is true.

Startingnodeurl is not set.

The specific node of the hierarchy.
Startfromcurrentnode is false.

Startingnodeurl has been set.

If the startingnodeoffset attribute is set to a non-0 value, it will affect the Start Node andSitemapdatasourceControls are based on the hierarchical structure of site map data exposed by the node. The value of startingnodeoffset is a negative integer or positive integer that identifies the level of the starting node identified by the startfromcurrentnode and startingnodeurl attributes move up or down along the hierarchy of the site map, to offset the Start Node of the subtree exposed by the data source control.

If the startingnodeoffset attribute is set to negative-N, the Start Node of the subtree exposed by the data source control is the top N level parent nodes of the identified Start Node. If the value of N is greater than all the upper-level levels above the Start Node identified in the hierarchy tree, the Start Node of the subtree is the root node of the hierarchy of the site map.

If the startingnodeoffset attribute is set to positive number + N, the starting node of the Public subtree is located at N levels under the identified starting node. Because the hierarchy may have branches of multiple subnodes, if possible,SitemapdatasourceThe child node will be parsed directly based on the path between the Start Node identified and the node indicating the current requested page. If the node on the requested page is not in the subtree of the Start Node, the value of the startingnodeoffset attribute is ignored. If the hierarchy gap between the nodes on the current requested page and the Start Node identified above is less than N levels, the current requested page is used as the Start Node.

Site map data is retrieved from sitemapprovider objects, such as xmlsitemapprovider, which is the default site map provider of ASP. NET. Can be specified as any provider of site configurationSitemapdatasourceProvides site map data and obtains a list of available providers by accessing the sitemap. Providers collection.

Like all data source controls,SitemapdatasourceEach instance is associated with a single helper object, which is called the data source view. Sitemapdatasourceview is a view based on site map data. It is set based on the data source attributes and retrieved by calling the gethierarchicalview method. Sitemapdatasourceview maintains the sitemapnodecollection object bound to the control.

By default, the Start Node is the root node of the hierarchy, but you can set the Start Node to any node. The Start node can be a node relative to the current position in the site map or relative to an absolute position. You can specify the Start Node by setting the startingnodeurl attribute.

SitemapdatasourceIt is dedicated to navigation data and does not support regular data source operations such as sorting, filtering, paging, or caching, or data record operations such as update, insert, or delete.
Example

The following code example demonstrates how to declareUse sitemapdatasourceControl binds the Treeview control to a site map. The map data of the site is retrieved from the root node level.

C #

Program code <% @ page Language = "C #" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML>
<Body>
<Form runat = "server">
<Asp:Sitemapdatasource
Id ="Sitemapdatasource1"
Runat = "server"/>

<Asp: Treeview
Id = "treeview1"
Runat = "server"
Performanceid ="Sitemapdatasource1 ">
</ASP: Treeview>

</Form>
</Body>
</Html>

Visual Basic
Program code <% @ page Language = "VB" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<Body>
<Form runat = "server">
<Asp:Sitemapdatasource
Id ="Sitemapdatasource1"
Runat = "server"/>

<Asp: Treeview
Id = "treeview1"
Runat = "server"
Performanceid ="Sitemapdatasource1 ">
</ASP: Treeview>

</Form>
</Body>
</Html> J # program code <% @ page Language = "VJ #" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML>
<Body>
<Form runat = "server">
<Asp:Sitemapdatasource
Id ="Sitemapdatasource1"
Runat = "server">
</ASP:Sitemapdatasource>

<Asp: Treeview
Id = "treeview1"
Runat = "server"
Performanceid ="Sitemapdatasource1 ">
</ASP: Treeview>

</Form>
</Body>
</Html>

Source: http://www.ad0.cn/netfetch/article.asp? Id = 285

Related Article

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.