SiteMapNodeCollection.IndexOf(SiteMapNode) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
搜尋指定的 SiteMapNode 物件,然後傳回在整個集合中第一個符合元素之以零起始的索引。
public:
virtual int IndexOf(System::Web::SiteMapNode ^ value);
public virtual int IndexOf (System.Web.SiteMapNode value);
abstract member IndexOf : System.Web.SiteMapNode -> int
override this.IndexOf : System.Web.SiteMapNode -> int
Public Overridable Function IndexOf (value As SiteMapNode) As Integer
參數
- value
- SiteMapNode
要在 SiteMapNodeCollection 中尋找的 SiteMapNode。
傳回
整個 value
中第一個出現 SiteMapNodeCollection 之以零為起始的索引 (如有找到),否則為 -1。
範例
下列程式碼範例示範如何使用 Contains 類別的 SiteMapNodeCollection 和 IndexOf 方法。 此程式碼會檢查兩個提供者和 MyXmlSiteMapProvider
, AspNetXmlSiteMapProvider
是否有根節點的子節點重複。
String providername1 = "AspNetXmlSiteMapProvider";
String providername2 = "MyXmlSiteMapProvider";
SiteMapProviderCollection providers = SiteMap.Providers;
if (providers[providername1] != null && providers[providername2] != null)
{
SiteMapProvider provider1 = providers[providername1];
SiteMapProvider provider2 = providers[providername2];
SiteMapNodeCollection collection1 = provider1.RootNode.ChildNodes;
SiteMapNodeCollection collection2 = provider2.RootNode.ChildNodes;
int matches = 0;
foreach (SiteMapNode node in collection1)
{
if (collection2.Contains(node))
{
Response.Write("Match found at " +
providername1 + ", index = " +
collection1.IndexOf(node) + " with " +
providername2 + ", index = " +
collection2.IndexOf(node) + ".<br />");
matches++;
}
}
Response.Write("Number of matches found = " +
matches.ToString() + ".");
}
else
{
Response.Write(providername1 + " or " +
providername2 + " not found.");
}
Dim providername1 As String = "xAspNetXmlSiteMapProvider"
Dim providername2 As String = "MyXmlSiteMapProvider"
Dim providers As SiteMapProviderCollection = SiteMap.Providers
If Not (providers(providername1) Is Nothing) AndAlso Not (providers(providername2) Is Nothing) Then
Dim provider1 As SiteMapProvider = providers(providername1)
Dim provider2 As SiteMapProvider = providers(providername2)
Dim collection1 As SiteMapNodeCollection = provider1.RootNode.ChildNodes
Dim collection2 As SiteMapNodeCollection = provider2.RootNode.ChildNodes
Dim matches As Integer = 0
Dim node As SiteMapNode
For Each node In collection1
If collection2.Contains(node) Then
Response.Write("Match found at " & _
providername1 & ", index = " & _
collection1.IndexOf(node) & " with " & _
providername2 & ", index = " & _
collection2.IndexOf(node) & ".<br />")
matches += 1
End If
Next node
Response.Write("Number of matches found = " & _
matches.ToString() + ".")
Else
Response.Write(providername1 & " or " & _
providername2 & " not found.")
End If
備註
方法會 IndexOf 呼叫 方法來 Object.Equals 判斷相等。