ASP.Net Menu Control Open in New Window

Today I was presented with a problem that seemed simple enough, how do we make an ASP.Net Menu control open in a new window.  Sounds easy right, well that is what I thought.  But before I go too far, lets back up a minute and review what I am working with.

Using ASP .Net 3.5 with VB.Net (C# would be preferred but someone started the app, besides it translates easy enough).

I have a .Net XML sitemap file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
	<siteMapNode url="Default.aspx" title="Home"  description="">
 
		<siteMapNode title="Membership"
					 url="~/Membership.aspx"
					 description="Membership Management"
					 roles="Admins"/>
 
		<siteMapNode url="CustomerSearch.aspx"
					 title="Customer Search"
					 description="Customer Search"
					 roles="Admins"/>
 
		<siteMapNode title="User" description="Display/Search for user" roles="IT">
			<siteMapNode title="User Search"
						 url="~/userLookup.aspx"
						 description="Search for Users"
						 roles="Admins"/>
 
			<siteMapNode title="Create User"
						 url="http://external/createUser.asp?window=new"
						 description="Create a new user"
						 roles="Admins"/>
      		</siteMapNode>
    	<siteMapNode title="Logout"
					 url="~/Logout.aspx"
					 description="Logout"
					 roles="Admins"/>
 
	</siteMapNode>
</siteMap>

And an ASP.Net Menu control:

1
2
3
4
5
6
7
8
<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1" 
                            Orientation="Horizontal" BackColor="#E9E1EA">
                        <LevelMenuItemStyles>
                            <asp:MenuItemStyle CssClass="topMenuItem" Font-Bold="True" 
                                Font-Underline="False" ItemSpacing="25px" VerticalPadding="4px"/>
                        </LevelMenuItemStyles>
                        <DynamicHoverStyle BackColor="#E9E1EA" />
                    </asp:Menu>

This is what I was given from here, it was pretty simple once my brain started to function again, I fired an event as the menu item’s were bound, checked for a parameter in the url passed in from the site map and if it was present I added the target of   ‘_blank’ to that menu item. And BOOM it worked, simple and a little more ghetto than I usually strive for but ultimatly an acceptable solution.  So here is what is looks like in code.

First thing you will notice is in my XML two of the menu item urls contain ‘window=new’, well this is the url parameter that my method is looking for, here is the XML in question:

1
2
3
4
5
6
7
8
9
<siteMapNode title="User Search"
		url="~/userLookup.aspx"
		description="Search for Users"
		roles="Admins"/>
 
<siteMapNode title="Create User"
		 url="http://external/createUser.asp?window=new"
		 description="Create a new user"
		 roles="Admins"/>

Once I got that figured out I added the event action to the ASP:

1
2
3
4
5
6
7
8
<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1" 
                            Orientation="Horizontal" BackColor="#E9E1EA" OnMenuItemDataBound="topMenu_menuItemDataBound" >
                        <LevelMenuItemStyles>
                            <asp:MenuItemStyle CssClass="topMenuItem" Font-Bold="True" 
                                Font-Underline="False" ItemSpacing="25px" VerticalPadding="4px"/>
                        </LevelMenuItemStyles>
                        <DynamicHoverStyle BackColor="#E9E1EA" />
                    </asp:Menu>

Once that was ready to go I wrote up the VB that lives behind the scenes, its pretty simple really:

1
2
3
4
5
Protected Sub topMenu_menuItemDataBound(ByVal sender As Object, ByVal e As MenuEventArgs) Handles Menu1.MenuItemDataBound
    If (e.Item.NavigateUrl.Contains("window=new")) Then
        e.Item.Target = "_blank"
    End If
End Sub

There is probably a more elegant way to do this but for the time period I had and the need it was perfect, if we ever need to expand this I would probably just create my own sitemap that had target as an attribute and then build an alternative menu builder to take into account target.