Tuesday, March 23, 2010

Default Button, asp.net

Default Button for a page:
                  <form id="form1" runat="server" defaultbutton="buttonName"></form>
Default Button for a panel:
                  <asp:Panel runat="server" DefaultButton="lbHello"></asp:Panel>
Default in a div:-
                  <div onkeypress="javascript:return WebForm_FireDefaultButton(event, 'buttonID')"></div>


For Linkbutton to make a default page, add below javascript at the end of the page

<script type="text/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;
}
</script>

No comments: