开发者

How can I prevent Firefox from opening the gridview header sort postback link in a new tab on Ctrl Click

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 nam开发者_运维百科e. 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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜