Hi ahmadsubuhanlubis,
Using this article i have created the example.
Please refer below sample code.
HTML
<asp:TextBox ID="txtMenu" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="OnSearch" />
<hr />
<asp:Menu ID="menu1" runat="server" DataSourceID="SiteMapDataSource1" Orientation="Horizontal"
OnMenuItemDataBound="OnMenuItemDataBound">
<LevelMenuItemStyles>
<asp:MenuItemStyle CssClass="main_menu" />
<asp:MenuItemStyle CssClass="level_menu" />
</LevelMenuItemStyles>
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />
Code
C#
protected void OnMenuItemDataBound(object sender, MenuEventArgs e)
{
if (SiteMap.CurrentNode != null)
{
if (e.Item.Text == SiteMap.CurrentNode.Title)
{
if (e.Item.Parent != null)
{
e.Item.Parent.Selected = true;
}
else
{
e.Item.Selected = true;
}
}
}
}
protected void OnSearch(object sender, EventArgs e)
{
string message = "Menu item not found.";
MenuItem item = menu1.FindItem(txtMenu.Text);
if (item != null)
{
message = item.Text + " found at depth " + item.Depth.ToString() + ".";
}
else
{
foreach (MenuItem menuItem in menu1.Items)
{
if (menuItem.Text.ToLower() == txtMenu.Text.ToLower())
{
message = menuItem.Text + " found at depth " + menuItem.Depth.ToString() + ".";
break;
}
if (SearchChildItem(txtMenu.Text, menuItem) != null)
{
MenuItem childItem = SearchChildItem(txtMenu.Text, menuItem);
message = childItem.Text + " found at depth " + childItem.Depth.ToString() + ".";
break;
}
}
}
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + message + "');", true);
}
private MenuItem SearchChildItem(string searchText, MenuItem menuItem)
{
foreach (MenuItem childItem in menuItem.ChildItems)
{
if (childItem.Text.ToLower() == searchText.ToLower())
{
return childItem;
}
else
{
SearchChildItem(searchText, childItem);
}
}
return null;
}
VB.Net
Protected Sub OnMenuItemDataBound(ByVal sender As Object, ByVal e As MenuEventArgs)
If SiteMap.CurrentNode IsNot Nothing Then
If e.Item.Text = SiteMap.CurrentNode.Title Then
If e.Item.Parent IsNot Nothing Then
e.Item.Parent.Selected = True
Else
e.Item.Selected = True
End If
End If
End If
End Sub
Protected Sub OnSearch(ByVal sender As Object, ByVal e As EventArgs)
Dim message As String = "Menu item not found."
Dim item As MenuItem = menu1.FindItem(txtMenu.Text)
If item IsNot Nothing Then
message = item.Text & " found at depth " + item.Depth.ToString() & "."
Else
For Each menuItem As MenuItem In menu1.Items
If menuItem.Text.ToLower() = txtMenu.Text.ToLower() Then
message = menuItem.Text & " found at depth " + menuItem.Depth.ToString() & "."
Exit For
End If
If SearchChildItem(txtMenu.Text, menuItem) IsNot Nothing Then
Dim childItem As MenuItem = SearchChildItem(txtMenu.Text, menuItem)
message = childItem.Text & " found at depth " + childItem.Depth.ToString() & "."
Exit For
End If
Next
End If
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
Private Function SearchChildItem(ByVal searchText As String, ByVal menuItem As MenuItem) As MenuItem
For Each childItem As MenuItem In menuItem.ChildItems
If childItem.Text.ToLower() = searchText.ToLower() Then
Return childItem
Else
SearchChildItem(searchText, childItem)
End If
Next
Return Nothing
End Function
Screenshot