Link to home
Start Free TrialLog in
Avatar of David C
David CFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.NET defaultbutton not working in Chrome and Firefox

Hi Experts, I have the code as below. I had been using this, since asp:panels were not working so I tried this and still cant get it to work. Can anybody point me in the right direction.
'function in root
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;
}


'.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   ClientScript.RegisterClientScriptInclude("FixFireDefault", "FireDefaultButtonFix.js")
End Sub


'.aspx
<div onkeypress="return FireDefaultButton(event, '<%= LinkButton1.ClientID %>')">
table with controls
</div>

Open in new window

Avatar of Alpesh Patel
Alpesh Patel
Flag of United States of America image

takwirirar

In your Javasript function when u are taking value in "defaultbutton"... plz try this code

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

best of luck
Quite weird that asp:panel is not accepting defaultbutton

If you want it for the full page then you can use this code

Page.Form.DefaultButton = Button1.ClientID;

If you insist on using JS try this link

http://www.devx.com/vb2themax/Tip/18846
Avatar of David C

ASKER

@Conceptinfotech - the code is not working
@masterpass - Page.Form.DefaultButton = LinkButton1.ClientID works when the page loads i.e. I can see the LinkButton being given focus and if I press enter at that time the code is executed, but as soon as I click into the textbox etc. it loses the focus then when I press enter nothing happens again.
Type something into the textbox and then try pressing enter. I think it will work then
Avatar of David C

ASKER

Thats what I mean. In chrome when the linkbutton has focus it is highlighted by an orange border when I put  in the username and password that orange border dissapears. When I press enter, nothing happens
Avatar of David C

ASKER

The Experts Exchange website uses aspx and pressing enter during logon works in Chrome,Firefox etc. Can they help !!!
Avatar of David C

ASKER

hi guys I've got all browsers to recognise the enter key being pressed but I cant call the function that handles the login in my aspx.vb. How do I call a function for example login()
<script type="text/javascript">
    function CheckKeyPress(key) {
        if (key == 13) {
            alert("Enter key pressed");
            return false;
        }
    }
	</script>


<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" onkeydown="return CheckKeyPress(event.keyCode);"></asp:TextBox>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David C
David C
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial