Ayo Softech

Friday, 28 June 2013

How To set Default Button for ENTER key pressed event(3 way)

1. <form id="form1"  runat="server" defaultbutton="Button1" defaultfocus="TextBox1">
2. <asp:Panel ID="Panel1" runat="server" DefaultButton="Button1"> 
3.Using Javascript :
var __defaultFired = false;
function WebForm_FireDefaultButton(event, target) {
    var element = event.target || event.srcElement;
    if (!__defaultFired && event.keyCode == 13 && !(element && (element.tagName.toLowerCase() == "textarea"))) {
        var defaultButton;

        if (__nonMSDOMBrowser)
            defaultButton = document.getElementById(target);
        else
            defaultButton = document.all[target];

        if (defaultButton) {
            if(typeof(defaultButton.click) != "undefined")
                defaultButton.click();
            else
                eval(unescape(defaultButton.href.replace("javascript:", "")));

            event.cancelBubble = true;

            if (event.stopPropagation) event.stopPropagation();
            return false;
        }
    }
    return true;
}

1 comment: