Home - Forums-.NET - FlyTreeView (ASP.NET) - Use Ctrl + Click on Link to open a new browser window

FlyTreeView (ASP.NET)

Technical support and KB related to the FlyTreeView control

This forum related to following products: FlyTreeView for ASP.NET

Use Ctrl + Click on Link to open a new browser window
Link Posted: 26-Mar-2007 00:42
Greetings,

in IE 7.0 it is possible to use a CTRL+Click to open a link in a new window (or a new tab).

FlyTreeView does not seem to support this feature. Any chance to emulate that behaviour?
Link Posted: 26-Mar-2007 02:46
Yes, there can be a possible solution (even cross-browser).

Here is helper javascript:

    var ctrlPressed = false;
    var ctrkKeyCode = 17;
    function openWindowCTRLEnabled(url)
    {
        var target = ctrlPressed ? \"_blank\" : \"_self\";
        window.open(url, target);
    }
    function handleKeyDown(evt)
    {
        var keyCode = window.event ? window.event.keyCode : evt.which;
        if (keyCode == ctrkKeyCode) ctrlPressed = true;
    }
    function handleKeyUp(evt)
    {
        var keyCode = window.event ? window.event.keyCode : evt.which;
        if (keyCode == ctrkKeyCode) ctrlPressed = false;
    }
    function registerHandler(target, type, handlerFunc)
    {
      if (document.addEventListener) {
        target.addEventListener(type, handlerFunc, false);
      } else if (document.attachEvent) {
        target.attachEvent(\"on\" + type,handlerFunc);
      } else {
        target[\"on\" + type] = handlerFunc;
      }  
    }
    registerHandler(document, \"keydown\", handleKeyDown);
    registerHandler(document, \"keyup\", handleKeyUp);


And also every node should replace its NavigateUrl to make it using openWindowCTRLEnabled instead of default action. E.g.:
[code][/code]
Link Posted: 27-Mar-2007 21:08
Thanks Evgeny,

doesn't work properly when populating nodes server side on demand though.


Moreover, I figured I might as well use the client side event handler method to achieve to goal, like so:


function onNodeEventHandler(treeview, node, eventType)
{    
            if(eventType==\"selected\")
            {
                if(ctrlPressed)
                {                    
                    ctrlPressed = false;
                    window.open(node.getNavigateUrl(), \"_blank\", \"\");
                }
            }
}


However, this does not stop the treeview from also taking the default action (namely, opening the navigateUrl in the defined navigateTarget).

Is there a way to clear the event cache, so if I handle 'selected' event myself, it will not execute the default action?
Link Posted: 27-Mar-2007 22:20
Do you just need to open URL only when user holds CTRL and do not open otherwise?

Then just need to change openWindowCTRLEnabled to

function openWindowCTRLEnabled(url)
{
    if (ctrlPressed)
    {
        window.open(url, \"_blank\");
    }
}


But you do not need to use onNodeEventHandler, just modify NavigateUrl to include javascript call.
Link Posted: 27-Mar-2007 22:43
[quote="EvgenyT"]But you do not need to use onNodeEventHandler, just modify NavigateUrl to include javascript call.


As I pointed out, trying to set that kind NavigateUrl when populating nodes server side does not work properly. It's beyond me, but all I get is a client side error:


Line: 1
Char: 1
Error: Object expected
Code: 0
Url: about:blank


The fact that the error's source is marked coming from line 1, char 1 doesn't exactly help solving the issue

Guess, I will have to find yet another work-around to achieve standard behaviour.
Link Posted: 27-Mar-2007 22:57
Works in load-on-demand examples ^)

I've just copied above javascript to Demo_Classic.aspx
and modified Demo_Classic.aspx.cs:

protected void flyTreeView_PopulateNodes(object sender, NineRays.WebControls.FlyTreeNodeEventArgs e)
{
    FileSystemNodes.AddNodes(e.Node.ChildNodes, e.Node.Path);
    foreach (NineRays.WebControls.FlyTreeNode node in e.Node.ChildNodes)
    {
        node.NavigateUrl = \"javascript:openWindowCTRLEnabled('http://www.9rays.net/')\";
    }
}


It works as it should. No errors, etc.
Link Posted: 27-Mar-2007 23:00
Finally also got some problems here.

Will try to solve.
Link Posted: 27-Mar-2007 23:30
Problems were in debug enabled in web.config.
So flytreeview built-in images were not properly cached.

Still do not get any javascript errors.
Link Posted: 28-Mar-2007 00:36
The problem seems to be related to setting .NavigateTarget. If I set .NavigateTarget to null your solution works properly, values like \"_blank\", \"_self\", \"\" will lead to the error described above.

Not sure if I still need NavigateTarget at all when using your solution, however I am still under the impression that this is some kind of bug.
Link Posted: 28-Mar-2007 01:39
Yes, that's the reason.
You should leave NavigateTarget null to make my code work.