摘要:[ASP.NET]動態修改SiteMap
每個人在做網站節點的時候都有自己的習慣,
以前是習慣用搭配遞迴用DB的方式叫出來,
後來改用.NET裡面的Web.sitemap,
主要是使用XML的方式來定義每個節點,
<!-- ==========權限系統========== -->
<siteMapNode url="" title="權限系統" description="權限系統">
<siteMapNode url="~/account.aspx" title="帳號管理" description="帳號管理">
<siteMapNode url="~/account_modify.aspx" title="修改" description="修改" />
<siteMapNode url="~/account_addnew.aspx" title="新增" description="新增" />
</siteMapNode>
<siteMapNode url="~/login_log.aspx" title="登入記錄" description="登入記錄" />
</siteMapNode>
<!-- ==========權限系統========== -->
<siteMapNode url="" title="權限系統" description="權限系統">
<siteMapNode url="~/account.aspx" title="帳號管理" description="帳號管理">
<siteMapNode url="~/account_modify.aspx" title="修改" description="修改" />
<siteMapNode url="~/account_addnew.aspx" title="新增" description="新增" />
</siteMapNode>
<siteMapNode url="~/login_log.aspx" title="登入記錄" description="登入記錄" />
</siteMapNode>
<!-- ==========權限系統========== -->
就像上例一樣,再搭配SiteMapPath的控制項就可以製作簡易的節點,
可是有一個問題就是遇到用get方式的動態頁面就無法處理,
但其實是可以解決的,在要處理get方式的那個頁面下,加入以下的程式,
Private Function ExpandForumPaths(ByVal sender As Object, ByVal e As SiteMapResolveEventArgs) As SiteMapNode
'動態修改sitemap
Dim currentNode As SiteMapNode = SiteMap.CurrentNode.Clone(True)
Dim tempNode As SiteMapNode = currentNode
Dim forumID As Integer = My.Request("id")
tempNode = tempNode.ParentNode
If Not (forumID = 0) And Not (tempNode Is Nothing) Then
tempNode.Url = tempNode.Url & "?id=" & forumID.ToString()
End If
Return currentNode
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
'在記憶體中委任修改的sitemap
AddHandler SiteMap.SiteMapResolve, AddressOf Me.ExpandForumPaths
'在記憶體中委任修改的sitemap
End If
End Sub
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
'在頁面unload狀態中,釋放記憶體中的委任
RemoveHandler SiteMap.SiteMapResolve, AddressOf Me.ExpandForumPaths
End Sub
'動態修改sitemap
Dim currentNode As SiteMapNode = SiteMap.CurrentNode.Clone(True)
Dim tempNode As SiteMapNode = currentNode
Dim forumID As Integer = My.Request("id")
tempNode = tempNode.ParentNode
If Not (forumID = 0) And Not (tempNode Is Nothing) Then
tempNode.Url = tempNode.Url & "?id=" & forumID.ToString()
End If
Return currentNode
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
'在記憶體中委任修改的sitemap
AddHandler SiteMap.SiteMapResolve, AddressOf Me.ExpandForumPaths
'在記憶體中委任修改的sitemap
End If
End Sub
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
'在頁面unload狀態中,釋放記憶體中的委任
RemoveHandler SiteMap.SiteMapResolve, AddressOf Me.ExpandForumPaths
End Sub
主要是在記憶體中去做動態委任,
當然結束後要做出釋放,否則所有的節點都會一直使用get的方式去連結