To avoid postback on enter key


To avoid the post back when you have more than one textbox it will back to previous button


Avoiding Enter button to do Postback





The first step is to check the key press event on onkeydown event on the page

function EnterKeycheck()
{
document.onkeydown = OnKeyDownCheck;
}


Then you have the method to catch the keydown

function OnKeyDownCheck(e)
{

var keyCode = e ? e.keyCode : window.event.keyCode;
if (keyCode == 13 || keyCode == 116)

{
// blocks enter and F5 or enter on any browser
if (!e)
window.event.keyCode = 0;

return false;

}
}


Related Articles

How to Avoid Re-Submitting Data on Page Refresh?

In this article. I will explain how to prevent re-submitting data on browser page reload or refresh in your asp.Net webform using C#. Sometimes we have some web pages through which we submit some user data to the database one after another on the same page. But the problem is, when we submit one user data and then refresh/reload our browser, what happens? The previously submitted data is re-submitted to the database. Here i am going to tell you how to prevent that.

More articles: Avoid postback Refresh page Page life cycle

Comments

Author: macxima02 Aug 2010 Member Level: Gold   Points : 0

Thanks, This is really needful

Author: Er. Ram Singh01 Sep 2010 Member Level: Gold   Points : 2

function EnterKeycheck()
{
document.onkeydown = OnKeyDownCheck;
}


Then you have the method to catch the keydown

function OnKeyDownCheck(e)
{

var keyCode = e ? e.keyCode : window.event.keyCode;
if (keyCode == 13 || keyCode == 116)

{
// blocks enter and F5 or enter on any browser
if (!e)
window.event.keyCode = 0;

return false;

}
}

Author: D.Jeya kumar(JK)01 Sep 2010 Member Level: Gold   Points : 0

Hey

Why did you copy pasted the same?



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: