我有一个转发器,列出了所有的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异常:“收藏是只读”。
我怎么能数据绑定的Repeater之前删除从集合中的节点?
你不应该需要CTYPE
Dim children = _
From n In SiteMap.CurrentNode.ChildNodes.Cast(Of SiteMapNode)() _
Where n.Url <> "/Registration.aspx" _
Select n
使用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 )
避免集合移除项因为这是始终慢。 除非你要通过多次循环被你筛选掉更好。
我得到了它与下面的代码的工作:
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
?