views:

83

answers:

1

I am trying to make my gridview control in ASP.Net do a multi sort based on if the user pressed Ctrl key when trying to sort by clicking on a column name. The problem is that when I am using Firefox, if I click on a column name with Ctrl key pressed, the browser tries to open "javascript:__doPostBack('ctl00$ContentPla..." link in a new tab. IE and Chrome both don't do this unless the link is a real link.

Is there a way I can prevent Firefox from opening the link in a new tab and still cause the page to postback normally?

Thanks.

A: 

You need to capture the event of the Ctrl key being pushed down, using document.onKeyDown.

In your event handler, check if 'Ctrl' (key code 17) was pressed, as follows:

function document_keyDown(e) {
    var KeyID = (window.event) ? event.keyCode : e.keyCode;
        if (KeyID == 17) { 
            ctrlDown = true;
        }
}

Here, I'm setting a 'ctrlDown' variable to true.

For the onKeyUp event, you can do the exact opposite:

function document_keyUp(e) {
    var KeyID = (window.event) ? event.keyCode : e.keyCode;
    if (KeyID == 17) { 
       ctrlDown = false;
    }
}

Then, in the click event of your column elements, you can check if Ctrl has been clicked or not:

function columnElement_click() {
    if (ctrlDown != undefined && ctrlDown == true)
        alert("Ctrl + Click Received");
    return false;
}

Make sure your column click handler returns false. Otherwise, the browser will execute the code, but then navigate to the address in the link's 'href' attribute.

Hope this helps.

(See also: http://www.geekpedia.com/tutorial138_Get-key-press-event-using-JavaScript.html)

jonathanconway
Thanks a lot for your reply. I will try to implement it.