Introduction
Menus are used to navigate thru the web sites. There are many ways to create the menus, like, using menu control with XML or Sitemap data sources etc. Besides these all, there is another control called as, data list control. The DataList control is like the Repeater control, used to display a repeated list of items that are bound to the control. However, the DataList control adds a table around the data items by default. The DataList control may be bound to a database table, an XML file, or another list of items. In this tutorial we bind the XML Menu file with data list control.
Behind binding and displaying the xml menu, we will create dataset and cache them for future use, so, that it increases speed. Keeping all these things in mind I divided the tutorial into following sections:
- XML menu file
- Using namespaces
- Creating the DataSet and Caching it
- Create the Data List Control
- Binding the dataset to the Data List Control
- Using the Data List’s Hyper Control to Display the Menu
XML Menu file
Below is the sample xml file which contains MenuItem tag and it has other two sub tags in it, called as text and URL. Menu tag is used to identify the individual menu and it sub tags, text and URL is used for menu text and menu URL respectively. See the sample xml file as follows:
-
-
<?xml version="1.0" encoding="utf-8" ?>
-
<Menu>
-
<MenuItem>
-
<Text>
-
Add User
-
</Text>
-
<Url>
-
http://azamsharp.net/Admin/AddUser.aspx
-
</Url>
-
</MenuItem>
-
<MenuItem>
-
<Text>
-
Add Location
-
</Text>
-
<Url>
-
http://azamsharp.net/Admin/AddLocation.aspx
-
</Url>
-
</MenuItem>
-
</Menu>
-
Using NameSpaces
In this tutorial we are using the dataset to retrieve the xml content and later we are binding the datasets with the data list. So to use the dataset, first we need to import the System.Data namespace as follows:
-
<%@ Import Namespace="System.Data" %>
System.Data namespace has a class called Dataset, which we are using in this tutorial.
Creating the DataSet and Caching it
First we create a dataset and load the XML file to it as follows:
-
<%@ Page Language="C#" Debug="true" %>
-
<%@ Import Namespace="System" %>
-
<%@ Import Namespace="System.Data"%>
-
-
<script runat="server">
-
private const string XML_MENU_FILE = "MenuAdmin.xml";
-
-
void Page_Load(Object sender, EventArgs e)
-
{
-
DataSet dsMenu = new DataSet();
-
dsMenu.ReadXml(Server.MapPath(XML_MENU_FILE));
-
}
Menus are displayed every time when the user views the page. In the page_load function we creating dataset and reading the same file always. This increase the load time and we are unnecessary reading the xml file always which is an expensive operation. So, the better idea is to load file once and use it multiple times. This can be achieved by using the caches as follows:
-
<%@ Page Language="C#" Debug="true" %>
-
<%@ Import Namespace="System" %>
-
<%@ Import Namespace="System.Data"%>
-
-
<script runat="server">
-
private const string XML_MENU_FILE_PATH = "MenuAdmin.xml";
-
private const string MENU_CACHE_KEY = "AdminMenu";
-
-
void Page_Load(Object sender, EventArgs e)
-
{
-
DataSet dsMenu = new DataSet();
-
if(Cache[MENU_CACHE_KEY] != null)
-
{
-
dsMenu = (DataSet) Cache[MENU_CACHE_KEY];
-
}
-
else
-
{
-
dsMenu.ReadXml(Server.MapPath(XML_MENU_FILE_PATH));
-
Cache.Insert(MENU_CACHE_KEY, dsMenu, new System.Web.Caching.CacheDependency(Server.MapPath(MENU_FILE)));
-
}
-
-
}
See the above code, we modified it a little bit compared to it previous version. In this, first we are checking whether there is menu data set in cache array (with MENU_CACHE_KEY key), if it exists we are casting the Menu dataset from the cache array to our dataset “dsMenu”.
If the cache array does not have menu dataset key, we are loading the xml file into dataset “dsmenu” and inserting the dataset into the cache array for future use.
Create the data list control
We create a DataList in an .aspx page. See the below data list control, the contents are as follows:
- Data list takes the parameters id, which is used to identify the data list uniquely from the other controls and runat=”server” specifies that data list executes on the server.
- <HeaderTemplate> element are rendered first and only once within the output
- <ItemTemplate> element are repeated for each “record” in the DataSet,
- <FooterTemplate> element are rendered once within the output
-
<html>
-
<body>
-
<form runat="server">
-
<asp:DataList id="menulist" runat="server">
-
<HeaderTemplate>
-
…
-
</HeaderTemplate>
-
<ItemTemplate>
-
…
-
</ItemTemplate>
-
<FooterTemplate>
-
…
-
</FooterTemplate>
-
</asp:DataList>
-
</form>
-
</body>
-
</html>
We can add styles to the data list as parameters as follows:
-
<asp:DataList id="menulist"
-
runat="server"
-
……………..
-
………………
-
………………
-
headerstyle-font-name="Verdana"
-
headerstyle-font-size="12pt"
-
headerstyle-horizontalalign="center"
-
headerstyle-font-bold="True"
-
itemstyle-backcolor="#778899"
-
itemstyle-forecolor="#ffffff"
-
footerstyle-font-size="9pt"
-
footerstyle-font-italic="True">
Binding the DataSet to the Data List Control
We created the data list, data set and loaded the data set with xml, now, the other important step remain is, binding the data set with the data list control. This is done as follows:
-
<%@ Page Language="C#" Debug="true" %>
-
<%@ Import Namespace="System" %>
-
<%@ Import Namespace="System.Data"%>
-
-
<script runat="server">
-
private const string XML_MENU_FILE_PATH = "MenuAdmin.xml";
-
private const string MENU_CACHE_KEY = "AdminMenu";
-
-
void Page_Load(Object sender, EventArgs e)
-
{
-
DataSet dsMenu = new DataSet();
-
if(Cache[MENU_CACHE_KEY] != null)
-
{
-
dsMenu = (DataSet) Cache[MENU_CACHE_KEY];
-
}
-
else
-
{
-
dsMenu.ReadXml(Server.MapPath(XML_MENU_FILE_PATH));
-
Cache.Insert(MENU_CACHE_KEY, dsMenu, new System.Web.Caching.CacheDependency(Server.MapPath(MENU_FILE)));
-
}
-
menulist.DataSource=dsMenu;
-
menulist.DataBind();
-
-
}
DataBind() method binds the data source (which in our case is a xml data set) with the data list.
Using the Data List’s Hyper Control to Display the Menu
Now, the final part comes into picture, try to remember the data list control contents, there is element, which is repeated for each “record” in the DataSet. This is what we want to show the menu contents, Right!,
Menus are links, when the user clicks on it, we are moved to destined URL. So, for the purpose of this tutorial, we use Hyperlink control. It has text and NavigateUrl properties; we will set value of these properties from the xml data source as follows:
-
<html>
-
<body>
-
<form runat="server">
-
<asp:DataList id="menulist" runat="server">
-
<ItemTemplate>
-
-
<asp:HyperLink Text = ‘<%# DataBinder.Eval(Container.DataItem,"Text") %>’ NavigateUrl = ‘<%# DataBinder.Eval(Container.DataItem,"Url") %>’ id="hlLinks" runat="server">
-
-
</ItemTemplate>
-
-
</asp:DataList>
-
</form>
-
</body>
-
</html>
Hi, this is Shiva Kumar. You can call me Shiva. I'm a developer like you, with 7 Years of experience on different technologies. Earlier I used to write articles for Computers Today. Now I started this web site to help each other ( or ) atleast contribute some thing from my end. I'm always a student, if you need any articles or help which is not there in this web site, please