一般管理系統往往需要通過許可權管理菜單(是否顯示,是否Enable),再加上導航樹控制項TreeView,形成靈活的導航控制方案。
採用MasterPage是一個不錯的思路,可以把Menu控制項和TreeView控制項都在其中放置,並且提供相應的資料來源。如果Menu不經常變化(正常情況)就可以把Menu及其子功能表做成靜態,而TreeView內容根據功能表項目來動態載入。
介面:
代碼:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterPagePerson : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
// 許可權控制
if (Session["Previlege"].ToString() == "Person")
{
Menu1.Enabled = false;
Menu2.Visible = false;
smsResultCheck.Provider = System.Web.SiteMap.Providers["smpResultCheck"];//menu3
}
else
{
Menu1.Enabled = true;
Menu2.Visible = true;
smsResultCheck.Provider = System.Web.SiteMap.Providers["smpResultCheck"];//menu3
}
//導航樹內容配置
if(Session["TreeVProvider"]!=null)
this.SiteMapDataSource1.Provider = System.Web.SiteMap.Providers[Session["TreeVProvider"].ToString()];
else
smsResultCheck.Provider = System.Web.SiteMap.Providers["smpResultCheck"];
}
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
Session["TreeVProvider"] = "CodeMaintain";
this.SiteMapDataSource1.Provider = System.Web.SiteMap.Providers[Session["TreeVProvider"].ToString()];
}
protected void Menu2_MenuItemClick(object sender, MenuEventArgs e)
{
string m = Menu2.SelectedItem.Text;
if (m.Equals("經費管理"))
{
Session["TreeVProvider"] = "ProjectFee";
this.SiteMapDataSource1.Provider = System.Web.SiteMap.Providers[Session["TreeVProvider"].ToString()];
}
}
protected void Menu3_MenuItemClick(object sender, MenuEventArgs e)
{
string m = Menu3.SelectedItem.Text;
if (m.Equals("部門審核"))
{
Session["TreeVProvider"] = "smpResultCheckDepartment";
this.SiteMapDataSource1.Provider = System.Web.SiteMap.Providers[Session["TreeVProvider"].ToString()];
}
}
}
運行:
當然,需要配置web.config。
<siteMap enabled="true ">
<providers>
<add name="CodeMaintain" description="SiteMap provider that reads in .sitemap files. " type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a " siteMapFile="CodeMaintain.sitemap "/>
<add name="ProjectFee" description="SiteMap provider that reads in .sitemap files. " type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a " siteMapFile="ProjectFee.sitemap "/>
<add name="smpResultCheck" description="SiteMap provider that reads in .sitemap files. " type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a " siteMapFile="ResultCheck.sitemap "/>
<add name="smpResultCheckDepartment" description="SiteMap provider that reads in .sitemap files. " type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a " siteMapFile="~/ResultCheck/DepartmentCheck.sitemap "/>
</providers>
</siteMap>