ASP.NET Advanced Site Functionality
In this tutorial you will learn advanced site functionality, Enhanced Page Framework, To create the Web.Sitemap file, Tracking Traffic with Site Counters and Going Mobile.
Enhanced Page Framework
The advanced functionalities offered by ASP.NET include site navigation and site counters. The built in navigation has three components:
1. Site Structure which is a programming interface that lists the hierarchical structure of the Website. The site map object exposes this interface using the components provided by the Site Map Provider. The default data store is usually a root level xml file named app.sitemap.
2. Site Navigation is a control that names each node defined in the site map and associates it with a URL.
3. Site Structure Display is a component that renders the site navigation user interface in an intelligent fashion and maps user friendly URLs to appropriate .aspx files.
The Site Map is a container for the hierarchical collection of nodes representing pages in an application. The default representation of a site map is Web.sitemap. This is an xml file.
To create the Web.Sitemap file:
1. Right click on the Website root folder and select Add New item…
2. Select a XmlFile and name it Web.sitemap
3. Open the file and add the following code to it.
<siteMap>
<siteMapNode title="Home" description="Home" url="~/default.aspx">
<siteMapNode title="Courses" description="Our Courses"
url="~/Courses.aspx">
<siteMapNode title="Web Application Development" description="Web Application Courses" url="~/webapp.aspx" />
<siteMapNode title="Windows Application Development" description="Windows Application Courses"
url="~/Winapp.aspx" />
</siteMapNode>
<siteMapNode title="Certifications" description="Certifications we offer" url="~/Certifications.aspx">
<siteMapNode title="Training" description="Training classes"
url="~/Training.aspx" />
<siteMapNode title="Microsoft" description="Microsoft certifications" url="~/Microsoft.aspx" />
<siteMapNode title="Oracle" description="Oracle Certifications"
url="~/Oracle.aspx" />
</siteMapNode>
</siteMapNode>
</siteMap>
4. Open the Default.aspx file and enter the following code to the page.
<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
InitSiteMap();
}
void InitSiteMap()
{
caption.Text = SiteMap.RootNode.Title;
foreach (SiteMapNode node in SiteMap.RootNode.ChildNodes)
{
ListItem LI = new ListItem();
LI.Text = node.Title;
LI.Value = node.Url;
MyLinks.Items.Add(LI);
}
}</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Site Map</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel Id= "MyPanel" runat="server"
backColor="#FFFFC0"
width="100%">
<h1> <asp:Label runat="server" ID="caption" valign="top" BackColor="#FFFFC0">ExForSys Site Map</asp:Label></h1>
</asp:Panel>
<table width="100%"><tr><td style="width:200;" bgcolor="lightcyan" valign="top">
<asp:BulletedList runat="server" ID="MyLinks" DisplayMode="HyperLink" />
</td><td style="Width:10;" bgcolor="lightcyan"></td><td valign="top"> Content of the Page</td></tr></table>
<asp:SiteMapDataSource runat="server" ID="MySiteMap" />
<asp:TreeView runat="server" DataSourceID="MySiteMap">
<RootNodeStyle Font-Bold="true" />
<ParentNodeStyle Font-Bold="true" />
<NodeStyle Font-Size="0.8em" />
<DataBindings>
<asp:TreeNodeBinding NavigateUrlField="url" TextField="title" /></DataBindings>
</asp:TreeView>
<asp:Menu runat="server" DataSourceID="MySiteMap" />
</form>
</body>
</html>
5. Press F5 to execute the program. The output will look as under:
Let us examine what we have done in the above code. We have used a Panel control to define a header and put a label in it and we have added a BulletedList control to display the contents of the SiteMap.
Click here to view sample code
Then we have used a TreeView control to display the same information in a different format.
Click here to view sample code
Finally we have used a Menu control to display the same information.
< asp:Menu runat="server" DataSourceID="ExForSysSiteMap" / >
Another type of control that can be used to display the same information is the SiteMapPath control. This control displays a navigation path and shows the current user the place he is in and displays links to the Home page. The entire control can be deployed and bound to a data source without writing a single line of code. The SiteMapPath is customizable. The links will appear as: Home > Courses > Web Application Development. The user can navigate back and forth at higher or lower levels of the hierarchy using the SiteMapPath controls navigation features.
Tracking Traffic with Site Counters
Another advanced control provided by ASP.NET 2.0 for site enhancement is the Site Counter. This is extremely useful in monitoring visitor activity. The two types of site activity normally performed by visitors are Impression and ClickThrough. Impression is a viewing of some specific content while ClickThrough is a user’s click to see a specific content. The site counters provide means to track these activities and collects the read and write related activities to a database via an ad hoc provider. Counter data can be collected using page view counters and server counters such as Hyperlink and AdRotator.
For counters to work, the site counter service must be enabled in the configuration file in the site counters section. A database must be set up before they are used and finally the provider must be set up and connected to the related database using the Web Administration tool.
A hyperlink control configured to track clicks would be coded as under:
< asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://www.exforsys.com" Text="Visit us at ExForSys for the best tutorials" counterclicks="true" counterName="Exforsys.com" >< / asp:HyperLink >
Apart from accessing the counters declaratively it is possible to access the counters programmatically also. The Static members of the Site counter class can be used for reading and writing data in the site counters. The GetRows method returns a DataSet object filled with the site counter table.
Site counter providers include the AccessDataSource provider and the SQLDataSource Provider. ADO.NET classes are used to read and write onto the tables. Site counter tables are named aspnet_SiteCounters by default.
Going Mobile
ASP.NET 1.x used the Microsoft Mobile Toolkit to code for mobile devices. In ASP.NET 2.0 all controls have a mobile interface. The infrastructure of the server control are now built on a control adapter architecture. There is an adapater for each device and the number of adapters can be added. Since the controls are derived from the adapters no special action is required to be performed. The adapter renders the control intelligently to the device.
The following code is a sample of the code for a nokia device.
< asp:Label id="MyLabel" runat="server"
Text="Welcome ExForSys.com"
Nokia:Text="Time to upgrade your Nokia phone!"
cssClass="StandardStyleClass"
Nokia:cssClass="SpecialNokiaStyleClass" / >
The Mechanisms help developers override the built in rendering for mobile devices and provide automatic support with standard controls. However there are some controls which are specifically built for mobile devices. For instance the PhoneLink and Pager controls are exclusively for mobile devices.
It is clear that the direction of development of Web application development is the creation of a seamless, supportive, codeless environment that helps rapid application development and ease of deployment.