Friday, August 26, 2011

Display the Menu Items using XML file in Aspnet

Hi all,
Today I am going to explain the topic "Display the MenuItems using XML file".
I think everyone's mind, might have a question that Why should I use XML file in all the times?

The Simple Answer is
"XML is a cross-platform, hardware and software independent, text based markup language, which enables
you to store data in a structured format by using meaningful tags. XML stores structured data in XML documents
that are similar to databases."


Implementation:

For this, you have to create an usercontrol named as "MenuBackground.ascx"
.ascx is a web user control file which is used to create our own controls and can be used anywhere in our Project.

Here I just create the Menu background user control.

MenuBackground.ascx:


<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MenuBackground.ascx.cs"
 Inherits="DotNetToolKit.MenuItems.MenuBackground" %>

Code behind for this user control is below:

MenuBackground.ascx.cs:

public partial class MenuBackground : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void getMenuItemsfromXML( )
        {
            HtmlTable tbMenuItems = new HtmlTable();
            tbMenuItems.Attributes.Add("cellpadding", "0");
            tbMenuItems.Attributes.Add("cellspacing", "0");
            tbMenuItems.Attributes.Add("width", "100%");
            tbMenuItems.Attributes.Add("style", "font-variant: small-caps;height: 23px; font-align: center;");

            HtmlTableRow trMenuItems = new HtmlTableRow();
            ArrayList arr = new Util().getXMLContentForMenu("MenuItems");
            int Cnt = 0;
            foreach (object obj in arr)
            {
                HtmlTableCell tc = (HtmlTableCell)obj;
                trMenuItems.Cells.Add(tc);
                Cnt++;
            }

            tbMenuItems.Rows.Add(trMenuItems);
            tMenu.Controls.Add(tbMenuItems);

        }

    }


In above code you observed the method definition "getMenuItemsfromXML( )", which actually declares in your Web Form that where
you have to use this user control.

So now it's the time to create our Web Form named "MenuPage.aspx". Here in this page , you need to use the user control and getting
access the method(getMenuItemsfromXML) from code behind itself.

MenuPage.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MenuPage.aspx.cs" Inherits="DotNetToolKit.MenuItemsPra.MenuPage" %>
<%@ Register TagName="TopMenu" TagPrefix="TM" Src="~/MenuItemsPra/TopMenu.ascx" %>   




    


    

Code behind for this page is

MenuPage.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
        {
            TopMenu1.getMenuItemsfromXML( );
        }


Here you just call the method "getMenuItemsfromXML( )". After calling this method in .aspx.cs page, the called method (method definition)
invokes which is placed in the .ascx.cs.

In User Control Code Behind file (MenuBackground.ascx.cs) you observed that there is a class name "Util" which actually sets the XML Data by using the Method "getXMLContentForMenu".

In this class, you must add the namespaces

Util.cs:

using System.Xml;

public class Util
    {
        public ArrayList getXMLContentForMenu(string sXMLName)
        {
            XmlDocument xContentDoc = new XmlDocument();
            xContentDoc.Load(HttpContext.Current.Server.MapPath("~/XML/" + sXMLName + ".xml"));
            XmlElement root = xContentDoc.DocumentElement;
            XmlNodeList nodes = root.SelectNodes("/MenuItems/MenuItem");
            ArrayList al_Content = new ArrayList();
            HtmlTableCell tcMenuItem = null;
            HtmlAnchor ahref = null;
            foreach (XmlNode node in nodes)
            {
                tcMenuItem = new HtmlTableCell();
tcMenuItem.Attributes.Add("style","border-right-style:solid;border-left-style:solid;border-right-width:1px;border-right-color:#8386C9;border-left-width:1px;border-left-color:#8386C9;text-align:center;");
                ahref = new HtmlAnchor();
                ahref.Attributes.Add("class", "color: White;text-decoration:none; text-align:center;");
                ahref.HRef = node["MenuItemLink"].InnerText;
                ahref.InnerText = node["MenuItemName"].InnerText;
                tcMenuItem.Controls.Add(ahref);
                al_Content.Add(tcMenuItem);
            }
            return al_Content;
        }

    }


In above Method definition the Arguement(sXMLName) is nothing but our actual XML File named as "MenuItems" which is declared at
the declaration time of the method in "MenuBackground.ascx.cs" file.

So the Final Step is your XML file


MenuItems.xml:


  
    http://google.com
    GOOGLE
  
  
    http://facebook.com
    FACEBOOK
  
  
    http://w3schools.com
    W3SCHOOLS
  
  
    http://tutorialspoint.com
    TUTORIALSPOINT
  
  
    http://academictutorials.com
    ACADEMICTUTORIALS
  



Set as startup page "MenuPage.aspx" and Run it.
you get the result. That's it Friends...

Thanks
BhaskarReddy

Labels: , , ,

2 Comments:

At August 26, 2011 at 1:00 PM , Anonymous Anonymous said...

very Clear Explanation

 
At September 3, 2011 at 1:33 PM , Anonymous Anonymous said...

Excellent It's working..

Thanks really it's useful

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home