close

I wrote a simple code to show How to use Asp.NET Sitemap by ExtJs ToolBar Control. This sample code is pretty easy , and may need to improve. IF you gat any idea, please write me the letter.( aqua.lau@gmail.com)

The project using ExtExtenders.dll 3.2.0.0 and .Net 3.5 . You may download ExtExtenders.dll  in codeplex here, and SampleCode here SourceCode .

ExtJsSiteMap 

 

Before we start , you may want to add the following code in web.confog first, then you can setup toolbox to use ExtExtenders control in toolbox.

 <add name="wwScriptCompressionModule"  type="ExtExtenders.Helpers.wwScriptCompressionModule,ExtExtenders"/>

 Here comes to my web.sitemap 's content

<?xml version="1.0" encoding="utf-8" ?>

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

     <siteMapNode url="default.aspx" title="default"  description="default">          

           <siteMapNode key="Menu1" url="" title="Menu1" description="Menu1">

                <siteMapNode key="MenuItem1" url="~/FD/FD110.aspx" title="MenuItem1" description="MenuItem1"/>

                <siteMapNode key="Menu1-1" url="" title="Menu1-1" description="Menu1-1">

                     <siteMapNode key="MenuItem1-1" url="~/FD/FD200.aspx" title="MenuItem1-1" description="MenuItem1-1"/>                

                </siteMapNode>

           </siteMapNode>          

           <siteMapNode key="Menu2" url="~/SK/Default.aspx" title="Menu2" description="Menu2"          

           <siteMapNode key="Menu2-1" url="" title="Menu2-1" description="Menu2-1">

                <siteMapNode key="MenuItem2-1" url="~/SK/SK150.aspx" title="MenuItem2-1" description="MenuItem2-1"/>

           </siteMapNode>

                <siteMapNode url="~/SK/SK110.aspx" title="MenuItem2"  description="MenuItem2" />

           </siteMapNode>          

     </siteMapNode>

</siteMap>

I place the ToolBar control in a UserControl called Menu.ascx which under project Shared folder.Remeber to set the AutoPostBack="true" to fire the OnMenuItemClicked even.

And then comes to the codebehind code

 

public partial class Shared_Menu : System.Web.UI.UserControl

{

    protected void Page_Load(object sender, EventArgs e)

    {

        BuildMenu(null, SiteMap.RootNode, null);

    }

 

    private void BuildMenu(ExtExtenders.Menu menu, SiteMapNode currentNode, ExtExtenders.MenuItem parentMenuItem)

    {

        for (int i = 0; i < currentNode.ChildNodes.Count; i++)

        {

            SiteMapNode loopNode = currentNode.ChildNodes[i];

 

            ExtExtenders.MenuItem item = new ExtExtenders.MenuItem();

            item = GenMenuItem(loopNode, !(loopNode.HasChildNodes));

 

            if (loopNode.HasChildNodes) //populate Menu

            {

                ExtExtenders.Menu newMenu = GenMenu(loopNode);

 

                if (loopNode.ParentNode == SiteMap.RootNode)//Level one             

                    ToolBar1.Menus.Add(newMenu); 

                else

                {

                    newMenu.Items.RemoveAt(0);

                    item.SubMenu = newMenu;

                    menu.Items.Add(item);

                }

                BuildMenu(newMenu, loopNode, item);

            }

            else ////populate node (MenuItem)

            {

                menu.Items.Add(item);

            }

        }

    }

 

    private ExtExtenders.MenuItem GenMenuItem(SiteMapNode node, bool isLeaf)

    {

        ExtExtenders.MenuItem menuItem = new ExtExtenders.MenuItem();

        menuItem.Text = node.Title;

        if (isLeaf) menuItem.url = node.Url;

        return menuItem;   

    }

 

    private ExtExtenders.Menu GenMenu(SiteMapNode node)

    {

        ExtExtenders.Menu menu = new ExtExtenders.Menu();

        menu.AddItem(node.Title);

        return menu;

    }

 

 

    protected void toolBar_MenuItemClicked(object sender, ExtExtenders.ToolBarEventArgs e)

    {

        if (e.MenuClicked.url != null)

            Response.Redirect(e.MenuClicked.url);

    }

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 sophieProfession 的頭像
    sophieProfession

    sophieProfession

    sophieProfession 發表在 痞客邦 留言(0) 人氣()