2008-08-15 67 views
1

我有一個Repeater,它列出了ASP.NET頁面上的所有web.sitemap子頁面。它的DataSourceSiteMapNodeCollection。但是,我不希望我的註冊表單頁面出現在那裏。如何從SiteMapNodeCollection中刪除節點?

Dim Children As SiteMapNodeCollection = SiteMap.CurrentNode.ChildNodes 

'remove registration page from collection 
For Each n As SiteMapNode In SiteMap.CurrentNode.ChildNodes 
If n.Url = "/Registration.aspx" Then 
    Children.Remove(n) 
End If 
Next 

RepeaterSubordinatePages.DataSource = Children 

SiteMapNodeCollection.Remove()方法引發

NotSupportedException異常: 「收藏是隻讀」。

如何在DataBinding Repeater之前從集合中刪除節點?

回答

1

你不應該需要CTYPE

Dim children = _ 
    From n In SiteMap.CurrentNode.ChildNodes.Cast(Of SiteMapNode)() _ 
    Where n.Url <> "/Registration.aspx" _ 
    Select n 
1

使用LINQ和.Net 3.5:

//this will now be an enumeration, rather than a read only collection 
Dim children = SiteMap.CurrentNode.ChildNodes.Where(_ 
    Function (x) x.Url <> "/Registration.aspx") 

RepeaterSubordinatePages.DataSource = children 

沒有LINQ的,但使用的.Net 2:

Function IsShown(n as SiteMapNode) as Boolean 
    Return n.Url <> "/Registration.aspx" 
End Function 

... 

//get a generic list 
Dim children as List(Of SiteMapNode) = _ 
    New List(Of SiteMapNode) (SiteMap.CurrentNode.ChildNodes) 

//use the generic list's FindAll method 
RepeaterSubordinatePages.DataSource = children.FindAll(IsShown) 

避免集合移除項因爲這是始終慢。除非你多次循環,否則你最好過濾。

0

我得到了它與下面的代碼工作:

Dim children = From n In SiteMap.CurrentNode.ChildNodes _ 
       Where CType(n, SiteMapNode).Url <> "/Registration.aspx" _ 
       Select n 
RepeaterSubordinatePages.DataSource = children 

有沒有更好的辦法,我沒有使用CType()

此外,這將兒童設置爲System.Collections.Generic.IEnumerable(Of Object)。有沒有一種很好的方式來獲得更強類型的東西,如System.Collections.Generic.IEnumerable(Of System.Web.SiteMapNode)或更好的System.Web.SiteMapNodeCollection