.NET Representation fires before Javascript onKeypress

I am trying to use onkeypress on an input type = "text" element to disable some javascript if the enter button is pressed. It works on most pages, but I also have some .NET custom controls.

The problem is that .NET submit fires before the onkeypress key is pressed. Does anyone know how to make onkeypress fire first?

If it helps, here is my javascript:

 function SearchSiteSubmit(myfield, e)
{
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    else 
        return true;
    if (keycode == 13)
    {
        SearchSite();
        return false;
    }
    else 
        return true;
}

      

0


source to share


3 answers


How do you assign javascript?

It should look like this:



<input id="TextID" type="text" onkeypress="return SearchSiteSubmit('TextID', event)" />

      

0


source


Javascript OnKeyPress

will always run first, this is more of a case where it finished its work before the page is sent back.



I would say rethink what happens and where .. What happens on the server side?

0


source


This is not a very clear question, so I'll give it a chance -

It looks like you are looking for the "enter" key press here. The problem is that the "enter" key is usually bound to the submit button on the form automatically by the browser, which means that when the user hits the enter button, you're submitting the form, not running the javascript you have here. What you have to do is make a global event handler that checks if "MyField" has focus when the enter button is pressed, and if so, then run javascript rather than submit the form. Hope I understood your question!

0


source







All Articles