Asp. NET Web site navigation and navigation controls how to use _ Practical tips

Source: Internet
Author: User

Website navigation?

The traditional website navigation needs us to be in the page to make the hyperlink the way realizes, when the page changes or moves the time need one by one to make the revision in each page, this can be very troublesome.
In the site to create a site map, that is, all the linked addresses in a special file for unified management, so that the management of the very aspects.
How to get the website to navigate? How do you do it?

Need to create a new sitemap file in VS, and then relate the sitemap file to the navigation controls we want, so that we can achieve the effect of navigation, we want to change an address, directly in the site map. sitemap file changes on the line.
How do I add a site map to vs?

Select Site Map to create a new site map in the new item in VS.
To use the site map, we need to add the sitemap file to the site root folder.
When you create a new sitemap file, the default code looks like this:

<?xml version= "1.0" encoding= "Utf-8"?> <sitemapxmlns=
"http://schemas.microsoft.com/AspNet/" sitemap-file-1.0 ">
  <sitemapnodeurl=" "title=" "description=" ">
    <sitemapnodeurl=" "title=" " Description= "" "/>
    <sitemapnodeurl=" "title=" "description=" "/>
  </siteMapNode>
</ Sitemap>

We can see from the code that the root element sitemap of the file contains the <siteMapNode> elements, these <siteMapNode> elements form a tree structure, the first layer is the homepage of the website.
A property sheet commonly used for <siteMapNode> elements.

Complex navigation in order to show more clearly, we can use a few more. mapmap files, that is, nested site map.
We are creating a new folder in this folder to build two site map files, Products.sitemap and Servers.sitmap, build Web.sitemap into the root directory of the Web site and link the other two. sitemap files with the Web.site sitemapfile attribute. The code looks like this:
Products.sitemap:

&lt;?xml version= "1.0" encoding= "Utf-8"?&gt; &lt;sitemap xmlns= "http://schemas.microsoft.com/AspNet/" 
    sitemap-file-1.0 "&gt; &lt;sitemapnode title=" Products description= "our product" url= "~/chap/products.aspx" &gt; &lt;sitemapnode url= "description=/&gt; &lt;sitemapnode" software for "~/chap/software.aspx" title= Software "url=" ~/chap/Hardwar E.aspx "title=" Hardware "description=" hardware provided "/&gt; &lt;/siteMapNode&gt; &lt;/siteMap&gt; services.sitemap file: &lt;?xml V Ersion= "1.0" encoding= "Utf-8"?&gt; &lt;sitemap xmlns= "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" &gt; &L T;sitemapnodeurlsitemapnodeurl= "~/chap/services.aspx" title= "Services" description= "service provided by us" &gt; &lt;sitemapnode Url= "~/chap/training.aspx" title= "training" description= "training"/&gt; &lt;sitemapnode url= "~/chap/consulting.aspx" title= "consultation" description= "Problem Consulting"/&gt; &lt;sitemapnode url= "~/chap/support.aspx" title= "support" description= "technical support"/&gt; &lt;/siteM 
 Apnode&gt; &lt;/siteMap&gt;

Web.sitemap file:

<?xml version= "1.0" encoding= "Utf-8"?> <sitemap xmlns= 
"http://schemas.microsoft.com/AspNet/" sitemap-file-1.0 "> <sitemapnodeurlsitemapnodeurl=" home.aspx "title=" Home "  
  description=" Home "> 
    <sitemapnode sitemapfile= "~/chap/products.sitemap"/> 
    <sitemapnodesitemapfilesitemapnodesitemapfile = "~/chap/services.sitemap"/>  
  </siteMapNode>  

The navigation map is finished, then the navigation map is displayed.
SiteMapPath Control Display Navigation

Drag the control directly to the page you want to navigate. Control is automatically bound to the navigation map.
Show:

TreeView Control Display Navigation

The TreeView control is typically used for tree-structured site navigation, which can be used to display XML, tabular, or relational data.
Basic operations:
treeview Each node is actually an object of a TreeNode class. you can programmatically add and modify TreeNode objects dynamically. You can bind through a data source control, such as by SiteMapDataSource air conditioning feeling love you or XmlDataSource control.
The Treeviewcollapseall () and ExpandAll () methods collapse and expand nodes. Use the Nodes.Add () method to add to the node to the control, and the Nodes.remove () method deletes the specified node.
DEMO:

Here just to familiarize yourself with the TreeView and not implement it, right-click on the TreeView operation, and only refresh the TreeView control, and data binding. Familiar with the TreeView just to get acquainted with the TreeView.
MyTreeView Code:

&lt;%@ pagelanguage= "C #" autoeventwireup= "true" codefile= "Menu.aspx.cs" inherits= "menu"%&gt; DOCTYPE HTML PUBLIC "-//w3c//dtdxhtml 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "&gt; &lt;html xmlns=" http://www.w3.org/1999/xhtml "&gt; &lt;head runat=" Server "&gt; &lt;title&gt;&lt;/title&gt; & Lt;/head&gt; &lt;body&gt; &lt;formidformid= "Form1" runat= "Server" &gt; &lt;div&gt; &lt;/div&gt; &lt;asp : Menu id= "Menu1" runat= "Server" &gt; &lt;Items&gt; &lt;asp:menuitem text= "rice teacher" value= "rice teacher" &gt;&lt;/asp:menuitem &gt; &lt;asp:menuitem text= "Phase Seven" value= "seven" &gt; &lt;asp:menuitem text= "Sister One" value= "Sister One" &gt;&lt;/asp:menuitem& 
      Gt &lt;asp:menuitem text= "Sister II" value= "Sister II" &gt;&lt;/asp:MenuItem&gt; &lt;/asp:MenuItem&gt; &lt;asp:menuitem Tex t= "Phase Eight" value= "eight" &gt; &lt;asp:menuitem text= "English Department" value= "English Department" &gt;&lt;/asp:MenuItem&gt; &lt;asp:menuitem Text = "Health department on duty" value= "health department on duty" &gt;&lt;/asp:menuitem&gt; &lt;/asp:MenuItem&gt; &lt;asp:menuitem text= "phase Nine" value= "phase Nine" &gt;&lt;/asp:MenuItem&gt; &lt;asp:m Enuitem text= "Phase 10" value= "phase 10" &gt;&lt;/asp:MenuItem&gt; &lt;asp:menuitem text= "11 period" value= "11 period" &gt;&lt;/asp:menuitem& 
    Gt  &lt;/Items&gt; &lt;/asp:Menu&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;

 c# Code:

Public partial class Chap_myTreeView:System.Web.UI.Page {//Remove current node protectedvoid Removenode_click (object sender 
     , EventArgs e) {//If the current node exists. 
      if (Mytreeview.selectednode!=null) {//Get the parent node of the current node. 
      TreeNode parentnode= myTreeView.SelectedNode.Parent; 
      Removes the current node. 
       if (parentnode!=null) {parentNode.ChildNodes.Remove (mytreeview.selectednode);}  
      else {myTreeView.Nodes.Remove (mytreeview.selectednode); 
  }}//Add the current node. 
 
    Protectedvoid Addnode_click (object sender, Eventargse) {//If the value to add the current node is empty, return. if (TxtAdd.Text.Trim (). 
    Length &lt;1) {return; 
     ///Establish the Childnode of the node and set the Value property. 
    TreeNode Chilenode = new TreeNode (); 
    Assign a value to the newly added node. 
    Chilenode.value = TxtAdd.Text.Trim (); 
    Determines whether a node is selected. 
    if (Mytreeview.selectednode!=null)//If the current node exists. 
      {//Add the new Childnode object to the current node. 
   MYTREEVIEW.SELECTEDNODE.CHILDNODES.ADD (Chilenode);   Txtadd.text = ""; 
      The else {//is added to the tree as the root node. 
      MYTREEVIEW.NODES.ADD (Chilenode); 
     Clear text box. 
    Txtadd.text = ""; 
  }//Fold all the trees. 
  Protectedvoid Flodnodes_click (object sender, EventArgs e) {mytreeview.collapseall ();//Fold the tree all up. 
  }//All expanded.  
  Protectedvoid Openallnode_click (object sender, EventArgs e) {mytreeview.expandall ();//all Expand the tree.  } 
}

Effect:

The menu control is the same as the TreeView, and you don't have to repeat it here.
The navigation in Word gives us a clear view of the main body of the article, and I think every programmer who has read a thick document has a deep understanding of word navigation. Clear and reasonable navigation effectively guide users to all corners of the site, Baidu Map allows us to find every piece of land with a name. An article said, SEO industry is always circulating such a classic word: "Content for the king, the chain for the emperor, the chain for the Imperial concubine, the key words for the phase, code for will, the structure for the city, updated for the Prince, Baidu throne." The navigation is not the link address, but also in the position of the Imperial concubine ....

Hope that this article on the ASP.net website navigation and navigation control articles hope to help you learn.

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.