我怎么可以删除节点从SiteMapNodeCollection?
题
我已经有了一个中继器,其中列出了所有的 web.sitemap
儿童页上ASP.NET 页。它的 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:"收集的只读".
我怎么可以删除该节点从收集数据绑定之前继?
解决方案
你不应该需要CType
Dim children = _
From n In SiteMap.CurrentNode.ChildNodes.Cast(Of SiteMapNode)() _
Where n.Url <> "/Registration.aspx" _
Select n
其他提示
使用皇宫和。净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
没有皇宫,但使用。净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 )
避免删除的项目集合,因为这是一直缓慢。除非你要的循环,过多次你最好过滤。
我得到了它的工作与代码如下:
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
?
不隶属于 StackOverflow