Adding a target in menu items (for target="_blank")

Topics: Developer Forum
Jun 18, 2009 at 9:05 PM

Hi,

I am trying to implement the ability to open the "url" of a menu item in a new browser, instead of always opening the link in _self.

I have added the target property to my WebPage class, and am able to put this into the "xml" that the menu generates, but I cannot get it to open in a new window.

I am adding an attribute target to the xml... so I am left with a node like this...

<siteMapNode title="Google" url="http://www.google.com" visible="True" VisibleInMenu="True" pageId="43546c81-91e7-4ead-9a49-268e1294e741" target="_blank" />

the target is NOT automatically inserted into the <a href that is generated, but I read on another site : http://www.velocityreviews.com/forums/t116450-websitemap-and-target-of-the-url.html

But adding the event handler in the skin file where the <asp:Menu is created is not allowed : An error "D:\Projects\...\App_Themes\...\Skin.skin(35): Build (web): The 'MenuItemDataBound' property of a control type System.Web.UI.WebControls.Menu cannot be applied through a control skin."

Has anyone successfully been able to implement targets?

Many thanks in advance!

Brad

Jun 18, 2009 at 9:32 PM

mmm, figured this one out...

I added code in my App_Code/Controls/PageBaseClass.cs ... Instead of in the skin file, it attaches the event at run time...

protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)
{
    string target = ((SiteMapNode)(e.Item.DataItem))["target"];
    if (target == "_blank")
    {
        e.Item.Target = target;
    }
}

protected void Page_PreRender(object sender, EventArgs e)
{
    if (website.Theme == "Arabic")
    {
        HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("body1");
        body.Attributes.Add("dir", "rtl");
    }
    
    Menu menu = (Menu)Master.FindControl("pnlTop").FindControl("Menu1");

    if (menu != null)
    {
        menu.MenuItemDataBound += Menu1_MenuItemDataBound;
    }
}

 

This works for me!

Cheers,
Brad