I've got a Repeater that lists all the web.sitemap
child pages on an ASP.NET page. Its DataSource
is a SiteMapNodeCollection
. But, I don't want my registration form page to show up there.
我有一个Repeater,列出了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
The SiteMapNodeCollection.Remove()
method throws a
SiteMapNodeCollection.Remove()方法抛出一个
NotSupportedException: "Collection is read-only".
NotSupportedException:“Collection是只读的”。
How can I remove the node from the collection before DataBinding the Repeater?
在DataBinding Repeater之前,如何从集合中删除节点?
3 个解决方案
#1
1
Your shouldn't need CType
你不应该需要CType
Dim children = _
From n In SiteMap.CurrentNode.ChildNodes.Cast(Of SiteMapNode)() _
Where n.Url <> "/Registration.aspx" _
Select n
#2
1
Using Linq and .Net 3.5:
使用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
Without Linq, but using .Net 2:
没有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 )
Avoid removing items from collections as that's always slow. Unless you're going to be looping through multiple times you're better off filtering.
避免从集合中删除项目,因为它总是很慢。除非你要多次循环,否则你最好过滤掉。
#3
0
I got it to work with code below:
我让它使用下面的代码:
Dim children = From n In SiteMap.CurrentNode.ChildNodes _
Where CType(n, SiteMapNode).Url <> "/Registration.aspx" _
Select n
RepeaterSubordinatePages.DataSource = children
Is there a better way where I don't have to use the CType()
?
有没有更好的方法我不必使用CType()?
Also, this sets children to a System.Collections.Generic.IEnumerable(Of Object)
. Is there a good way to get back something more strongly typed like a System.Collections.Generic.IEnumerable(Of System.Web.SiteMapNode)
or even better a System.Web.SiteMapNodeCollection
?
此外,这会将子项设置为System.Collections.Generic.IEnumerable(Of Object)。是否有一种很好的方法可以获得类似System.Collections.Generic.IEnumerable(Of System.Web.SiteMapNode)更强类型的内容,甚至更好的System.Web.SiteMapNodeCollection?
#1
1
Your shouldn't need CType
你不应该需要CType
Dim children = _
From n In SiteMap.CurrentNode.ChildNodes.Cast(Of SiteMapNode)() _
Where n.Url <> "/Registration.aspx" _
Select n
#2
1
Using Linq and .Net 3.5:
使用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
Without Linq, but using .Net 2:
没有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 )
Avoid removing items from collections as that's always slow. Unless you're going to be looping through multiple times you're better off filtering.
避免从集合中删除项目,因为它总是很慢。除非你要多次循环,否则你最好过滤掉。
#3
0
I got it to work with code below:
我让它使用下面的代码:
Dim children = From n In SiteMap.CurrentNode.ChildNodes _
Where CType(n, SiteMapNode).Url <> "/Registration.aspx" _
Select n
RepeaterSubordinatePages.DataSource = children
Is there a better way where I don't have to use the CType()
?
有没有更好的方法我不必使用CType()?
Also, this sets children to a System.Collections.Generic.IEnumerable(Of Object)
. Is there a good way to get back something more strongly typed like a System.Collections.Generic.IEnumerable(Of System.Web.SiteMapNode)
or even better a System.Web.SiteMapNodeCollection
?
此外,这会将子项设置为System.Collections.Generic.IEnumerable(Of Object)。是否有一种很好的方法可以获得类似System.Collections.Generic.IEnumerable(Of System.Web.SiteMapNode)更强类型的内容,甚至更好的System.Web.SiteMapNodeCollection?