3
votes

I have a function that set focus to the next field via JS on the form when press enter:

function pressEnter(obj, e) {
    var key = e.keyCode ? e.keyCode : e.which;
    if (key === 13) {
        var total = document.forms[0].elements.length;
        for (var i = 0; i < total; i++) {
            if (obj.id == document.forms[0].elements[i].id) {
                index = i + 1;
                while ((document.forms[0].elements[index].type == "hidden" || document.forms[0].elements[index].nodeName == "FIELDSET" || document.forms[0].elements[index].disabled == true)) {
                    index++;
                    if (document.forms[0].elements[index] == null)
                        return;
                }
                $('#' + document.forms[0].elements[index].id).focus();
                break;
            }
        }
    }
}

In BODY tag, i have the following script: onkeypress = "var key = event.keyCode? event.keyCode: e.which; return (key! = 13);", to avoid the post when the [Enter] is pressed

Operate normally, if I set [TextBox1 autoPostBack=false] [TextBox2 autoPostBack=false]. Type in TextBox1, [enter], TextBox2 receives focus and triggers the onkeypress event of the body.

Good.

Here comes the problem

If I set [TextBox1 autoPostBack=false] [TextBox2 autoPostBack=true] when textBox2 receives the focus, it refreshes the page, executes the post, and do not trigger the onkeypress event of the body. It was not typed anything in TextBox2. I would like the TextBox with AutoPostBack = true does not make the post, or refresh the screen, until a value is entered and run the onBlur.

Detail, if press the TAB, this problem does not happen, just when set the focus programmatically via JS.

If someone can explain to me why, I'll be very grateful.

Tarsis - Varois

1
include your aspx markupAllen Rice
Try adding return false; to the pressEnter function and hopefully it will stop the ASP.NET auto postback code from firing.Shadow Wizard Is Vaccinated V3
Thanx for all answers. I did solve my problem but i couldn't find the reason this was happening. To solve the problem, i cahnge the property ClientIdMode to AutoID in all fields that have AutoPostBack=true. That's it .. my form stop triggering the POST when those fields receive the focus.Varois

1 Answers

0
votes

you need to have <form onsubmit="return false;"> for your form as mentioned here: How to prevent ENTER keypress to submit a web form?