Asp.net 如何从SiteMapNodeCollection中删除节点?

Asp.net 如何从SiteMapNodeCollection中删除节点?,asp.net,.net,vb.net,repeater,sitemap,Asp.net,.net,Vb.net,Repeater,Sitemap,我有一个转发器,它列出ASP.NET页面上的所有web.sitemap子页面。它的DataSource是一个SiteMapNodeCollection。但是,我不希望我的注册表格页面出现在那里 Dim Children As SiteMapNodeCollection = SiteMap.CurrentNode.ChildNodes 'remove registration page from collection For Each n As SiteMapNode In SiteMap.Cu

我有一个转发器,它列出ASP.NET页面上的所有
web.sitemap
子页面。它的
DataSource
是一个
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:“集合是只读的”


在对中继器进行数据绑定之前,如何从集合中删除节点?

使用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 
//这将是一个枚举,而不是只读集合
Dim children=SiteMap.CurrentNode.ChildNodes.Where(_
函数(x)x.Url“/Registration.aspx”)
RepeaterSubordinationPages.DataSource=子级
不使用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 )
函数显示为布尔值(n作为SiteMapNode)
返回n.Url“/注册.aspx”
端函数
...
//获取通用列表
将子项设置为列表(SiteMapNode的)=_
(SiteMapNode的)新列表(SiteMap.CurrentNode.ChildNodes)
//使用泛型列表的FindAll方法
RepeaterSubordinationPages.DataSource=children.FindAll(IsShown)

避免从集合中删除项目,因为这样做总是很慢。除非您要多次循环,否则最好进行过滤。

我使用下面的代码:

Dim children = From n In SiteMap.CurrentNode.ChildNodes _
               Where CType(n, SiteMapNode).Url <> "/Registration.aspx" _
               Select n
RepeaterSubordinatePages.DataSource = children
Dim children=来自SiteMap.CurrentNode.ChildNodes中的n_
其中CType(n,SiteMapNode).Url“/Registration.aspx”_
选择n
RepeaterSubordinationPages.DataSource=子级
有没有更好的方法不必使用
CType()


此外,这会将子对象设置为
System.Collections.Generic.IEnumerable(对象的)
。有没有一种更好的方法可以返回更强类型的内容,比如
System.Collections.Generic.IEnumerable(属于System.Web.SiteMapNode)
,或者更好的方法是
System.Web.SiteMapNodeCollection

您不需要CType

Dim children = _
    From n In SiteMap.CurrentNode.ChildNodes.Cast(Of SiteMapNode)() _
    Where n.Url <> "/Registration.aspx" _
    Select n
Dim子项=_
从SiteMap.CurrentNode.ChildNodes.Cast(SiteMapNode的)()中的n开始_
其中n.Url“/Registration.aspx”_
选择n