ASP. NET 2.0 data Tutorial: SiteMapDataSource Control

Source: Internet
Author: User

In asp.net 2.0, we can access data in multiple programming methods like asp.net 1.x, or through new data source controls.

There are multiple built-in data source controls, such as the SqlDataSource control used to access relational database data and the ObjectDataSoruce control used to access the data provided by the dataset class. You can also create your own custom data source controls.

The data source control acts as the proxy for your aspx page and underlying data. To display the data queried by the data source control, we need to add other Web controls to the page and bind them to the data source control. To bind a Web control to a data source control, you only need to set the DataSourceID attribute value of the Web control to the ID attribute value of the data source control.

To obtain the data in the site map, asp.net provides the SiteMapDataSource control, which allows us to bind a Web control to display our site map. The TreeView and Menu Web controls are often used to provide navigation user interfaces. To bind the data in the site map to these two controls, add a SiteMapDataSource control to the page and set the performanceid attribute value of the TreeView or Menu control to the ID attribute value of the SiteMapDataSource control. For example, we can use the following labels to add the Menu control to the motherboard page:

 
 
  1. < div id="navigation">  
  2.     < asp:Menu ID="Menu1" runat="server" 
  3.       DataSourceID="SiteMapDataSource1">  
  4.     < /asp:Menu>  
  5.  
  6.     < asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />  
  7. < /div>  

To generate optimized HTML, We can bind the SiteMapDataSource control to the Repeater control, as shown below:

 
 
  1. < div id="navigation">  
  2.      < ul>  
  3.          < li>< asp:HyperLink runat="server" ID="lnkHome" 
  4.           NavigateUrl="~/Default.aspx">Home< /asp:HyperLink>< /li>  
  5.    
  6.          < asp:Repeater runat="server" ID="menu" 
  7.            DataSourceID="SiteMapDataSource1">  
  8.              < ItemTemplate>  
  9.                  < li>  
  10.                     < asp:HyperLink runat="server" 
  11.                       NavigateUrl="< %# Eval("Url") %>">  
  12.                       < %# Eval("Title") %>  
  13.                     < /asp:HyperLink>  
  14.                 < /li>  
  15.             < /ItemTemplate>  
  16.         < /asp:Repeater>  
  17.     < /ul>  
  18.  
  19.     < asp:SiteMapDataSource ID="SiteMapDataSource1" 
  20.       runat="server" ShowStartingNode="false" />  
  21. < /div> 

The SiteMapDataSource control returns the level 1 of the Site Map level each time, starting from the root node in the site map (Home in our site map), and then the next level (Basic Reporting, filtering Reports and Customized Formatting.

When SiteMapDataSource is bound to the Repeater, it traverses the first level and displays each SiteMapNode instance at the first level using ItemTemplate. We can use Eval (attribute name) to access the details of SiteMapNode, so that we can get the Url and Title attributes of SiteMapNode to the HyperLink control.

The following shows the HTML Tag generated using the Repeater control example above:

 
 
  1. < li>  
  2.      < a href="/Code/BasicReporting/Default.aspx">Basic Reporting< /a>  
  3.  < /li>  
  4.    
  5.  < li>  
  6.      < a href="/Code/Filtering/Default.aspx">Filtering Reports< /a>  
  7.  < /li>  
  8.    
  9.  < li>  
  10.     < a href="/Code/CustomFormatting/Default.aspx">  
  11.      Customized Formatting< /a>  
  12. < /li> 

As shown above, the second-level nodes (Basic Reporting, Filtering Reports, and Customized Formatting) of the site map are displayed instead of the first node.

This is because the ShowStartingNode attribute of the SiteMapDataSource control is set to false. As a result, SiteMapDataSource skips the root node of the site map and returns information starting from the second level of the Site Map level.

To display the Basic Reporting, Filtering Reports, and sitmized Formatting subsitemapnode, we can add another Repeater to the ItemTemplate of the previous Repeater. The second Repeater is bound to the subnode attribute of the SiteMapNode instance, as follows:

 
 
  1. < asp:Repeater runat="server" ID="menu" DataSourceID="SiteMapDataSource1">  
  2.     < ItemTemplate>  
  3.         < li>  
  4.             < asp:HyperLink runat="server" 
  5.             NavigateUrl="< %# Eval("Url") %>">  
  6.             < %# Eval("Title") %>< /asp:HyperLink>  
  7.  
  8.              < asp:Repeater runat="server" 
  9.               DataSource="< %# ((SiteMapNode)  
  10.              Container.DataItem).ChildNodes %>">  
  11.                 < HeaderTemplate>  
  12.                     < ul>  
  13.                 < /HeaderTemplate>  
  14.  
  15.                 < ItemTemplate>  
  16.                     < li>  
  17.                         < asp:HyperLink runat="server" 
  18.                          NavigateUrl="< %# Eval("Url") %>">  
  19.                          < %# Eval("Title") %>< /asp:HyperLink>  
  20.                     < /li>  
  21.                 < /ItemTemplate>  
  22.  
  23.                 < FooterTemplate>  
  24.                     < /ul>  
  25.                 < /FooterTemplate>  
  26.             < /asp:Repeater>  
  27.         < /li>  
  28.     < /ItemTemplate>  
  29. < /asp:Repeater> 

The HTML tags generated by the two Repeater are removed to save space ):

 
 
  1. < li>  
  2.      < a href="/Code/BasicReporting/Default.aspx">Basic Reporting< /a>  
  3.      < ul>  
  4.         < li>  
  5.            < a href="/Code/BasicReporting/SimpleDisplay.aspx">  
  6.              Simple Display< /a>  
  7.         < /li>  
  8.         < li>  
  9.            < a href="/Code/BasicReporting/DeclarativeParams.aspx">  
  10.             Declarative Parameters< /a>  
  11.        < /li>  
  12.        < li>  
  13.           < a href="/Code/BasicReporting/ProgrammaticParams.aspx">  
  14.             Setting Parameter Values< /a>  
  15.        < /li>  
  16.     < /ul>  
  17. < /li>  
  18.  
  19. < li>  
  20.     < a href="/Code/Filtering/Default.aspx">Filtering Reports< /a>  
  21.       
  22. < /li>  
  23.  
  24. < li>  
  25.     < a href="/Code/CustomFormatting/Default.aspx">  
  26.       Customized Formatting< /a>  
  27.       
  28. < /li> 

The CSS style selected from The book of Rachel Andrew: The CSS Anthology: 101 Essential Tips, Tricks, & Hacks, <ul> and <li> elements are shown as follows:

 

Figure 11: Display menus with two Repeater and some CSS

This menu is defined on the dashboard page and is bound to the Web. the Site map defined in sitemap, which means that changes to all Site maps will immediately reflect that all sites are used. master board page.

Turn off view status

All asp.net controls can maintain their State to View State at will. Note: ViewState with uppercase letters at the beginning will not be translated, when HTML is generated, it is serialized and stored in a hidden form field. Controls use ViewState to remember the state changed by the program when they are returned on the page, such as the data bound to the Web control. If the view status allows information to be maintained when the page is returned, it will increase the size of HTML code sent to the client. Without precise monitoring, the page will expand greatly. The data display control, especially the GridView control, will significantly add a large number of additional tags to the page. Of course, these increases may have no impact on broadband users, but the view status will increase the latency of dial-up users by several seconds.

To observe the impact of view status, open the page in the browser and view the source code of the page (for Internet Explorer, click "View" and select the source code option ). You can also open the page tracking option to observe the view status of each control on this page. View status information is serialized and placed in a hidden form field named _ viewstate in the <div> element following the <form> tag.

The view status is maintained only when Form is used on the page. If your aspx page does not contain

<Form runat = "server">, the resulting HTML tag does not contain VIEWSTATE hidden form fields.

The VIEWSTATE hidden form field generated on the dashboard page contains approximately 1800 bytes. The additional data is mainly generated by the SiteMapDataSource control for the data content provided by the Repeater control. Maybe about 1800 bytes does not look much, but it is easy to expand 10 times or more when using the GridView and using many fields and recorded view states.

You can set the EnableViewState attribute to false to disable the view State at the page or control level, thus reducing the size of the generated tag. The Web control uses the view status to maintain the data to be bound to the data display control when the page returns. When the view status of the data display control is disabled, you must re-bind data to the control each time the page returns. In asp.net 1.x, this responsibility falls on the developer. In asp.net 2.0, when the page returns, the data display control rebinds the data when necessary.

Setting the EnableViewState of the Repeater control to false can reduce the view State of the page. You can set it in the Properties window or manually modify it in the Code view. With these changes, the Repeater flag will look like this:

 
 
  1. < asp:Repeater runat="server" ID="menu" DataSourceID="SiteMapDataSource1"   
  2. EnableViewState="False">  
  3.     < ItemTemplate>  
  4.          < i>ItemTemplate contents omitted for brevity< /i>   
  5.     < /ItemTemplate>  
  6. < /asp:Repeater>  

After these changes, the view status generated on the page is reduced to 52 bytes, reducing the view status data by 97%! In this Guide series, I will disable the view status of all data controls to reduce the size of the tags generated. In most cases, the EnableViewState attribute is set to false without a prompt.

It is discussed only when the data Web control must open its view status to provide the expected functions.

  1. ASP. net mvc Path Selection System Construction
  2. URL path selection scenario in ASP. net mvc Framework
  3. URL path selection rules of ASP. net mvc Framework
  4. ASP. net mvc Framework: Use a strong type class to pass ViewData
  5. Use ASP. net mvc framework to create an e-commerce website

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.