开发者

Setup asp.net default enter press client side

开发者 https://www.devze.com 2023-01-22 03:31 出处:网络
Working wi开发者_如何学Pythonth asp.net, and I am trying to find a way to change default ENTER key press target on client side. Based on something like the currently focused div or input.

Working wi开发者_如何学Pythonth asp.net, and I am trying to find a way to change default ENTER key press target on client side. Based on something like the currently focused div or input.

The target would be asp.net button controls with postback event.

Looking for solution for both IE and Firefox.

I am also using WebForm_FireDefaultButton(event, controlId) trying to set the default button but it doesn't work for Firefox; work fine for IE though.


Look at the Default Button property of an ASP.NET Panel Control


It's too difficult to answer this question without knowing the context. Generally speaking you must prepare javascript with required actions and then add something like

onkeydown = "if (event.keyCode == 13) submitForm(); if (event.keyCode == 27) cancelLogin();"

on client side or something similar on server side to action producing controls. The above example refers to MVC. For WebForms you can use __doPostBack instead of my submitForm() function (link text). Usually I used such scenario with inputs (TextBox controls for WebForms).


here is the cause and solution i just found Asp.Net Form DefaultButton Error in Firefox

basically, on whatever event you prefer, call the following "FireDefaultButton" function to set the default fire button. The out of box function "WebForm_FireDefaultButton" does not compatible with Firefox (see the link for detail)

function FireDefaultButton(event, target) 
{
// srcElement is for IE
var element = event.target || event.srcElement;

if (13 == event.keyCode && !(element && "textarea" == element.tagName.toLowerCase())) 
{
    var defaultButton;
    defaultButton = document.getElementById(target);

    if (defaultButton && "undefined" != typeof defaultButton.click) 
    {
        defaultButton.click();
        event.cancelBubble = true;
        if (event.stopPropagation) 
            event.stopPropagation();
        return false;
    }
}
return true;
}
0

精彩评论

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