3

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

Varois
  • 131
  • 3
  • 16
  • include your aspx markup – Allen Rice Jun 03 '11 at 19:25
  • Try adding `return false;` to the `pressEnter` function and hopefully it will stop the ASP.NET auto postback code from firing. – Shadow The GPT Wizard Jun 03 '11 at 20:30
  • 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 Jun 17 '11 at 13:05

1 Answers1

0

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

Community
  • 1
  • 1
Akhil
  • 7,570
  • 1
  • 24
  • 23
  • Well, basically, my question wans't to prevent Form Submit wicth Enter. I am already doing it with that small code in BODY tag. so When i press ENTER nothing happen other than focus in next field. If i use your code, i not gonna be able to submit the form any more. and this is not the case .. The submit is happening when a asp:TextField, set with autoPostBack=true and onTextChanged="someFunction", receive a focus. but for me, onTextChanged, just trigger when you leave the field. Thanks anyway. – Varois Jun 03 '11 at 20:02
  • onTextChanged is a server side method, and should'nt fire untill form posts back. Are you using UpdatePanel by any chance? – Akhil Jun 03 '11 at 20:15
  • Whats the update Mode Set To? and can you set ChildrenAsTriggers= false? – Akhil Jun 03 '11 at 20:40
  • Now, i'm not setting none of them .. but i've tried both: always and conditional – Varois Jun 03 '11 at 20:43
  • did you try ChildrenAsTriggers = false? – Akhil Jun 03 '11 at 20:48