I have a standard asp.net webform with multiple textboxes (Autopostback=False) on it and a Save button.
Pressing ENTER while in any textbox is causing the server side click event of the Save button to be invoked. If I break and look at the call stack, the click event is the only event listed. I do not have a default button set for the form. If I put another button above the save button, then it gets its click event invoked on any press of enter on a textbox.
我有一个标准的asp.net webform,上面有多个文本框(Autopostback = False)和一个Save按钮。在任何文本框中按ENTER键会导致调用“保存”按钮的服务器端单击事件。如果我中断并查看调用堆栈,则单击事件是列出的唯一事件。我没有为表单设置默认按钮。如果我在保存按钮上方放置另一个按钮,则在文本框上输入的任何按下时都会调用其click事件。
Any ideas why this is happening, and how to get it to stop?
任何想法为什么会发生这种情况,以及如何让它停止?
5 个解决方案
#1
This is the default behaviour for most fields except for text areas.
这是除文本区域以外的大多数字段的默认行为。
To get around this you can call a javascript function before the form is submitted to check the keypress.
为了解决这个问题,您可以在提交表单之前调用javascript函数来检查按键。
<script type="text/javascript">
function allowSubmission() {
return !(window.event && window.event.keyCode == 13); }
</script>
Then the only way to submit the form is to actually hit submit. However as many people have mentioned, the enter key submitting a form is expected behaviour, so you can always alter the function to do basic validation on the fields, allowing the enter key to submit the form if all the required fields have been filled in.
那么提交表单的唯一方法就是实际点击提交。然而,正如许多人所提到的,提交表单的输入键是预期的行为,因此您可以随时更改函数以对字段进行基本验证,如果已填写所有必填字段,则允许输入键提交表单。
<script type="text/javascript">
function allowSubmission() {
return !(window.event && window.event.keyCode == 13) || validateInput(); }
</script>
Edit: You'd call this function on the OnClientClick method of your submit button. Something like:
编辑:您将在提交按钮的OnClientClick方法上调用此函数。就像是:
<asp:Button id="SubmitBtn" runat="server" OnClientClick="return allowSubmission()"/>
#2
This is true for any webform: if you press enter while an input field has focus, the form will be submitted (except textareas). To stop that, you'll need some JavaScript to capture that event and prevent the form being submitted. Be aware though, pressing enter to submit is expected behavior for many users!
对于任何webform都是如此:如果在输入字段具有焦点时按Enter键,则将提交表单(textareas除外)。要阻止这种情况,您需要一些JavaScript来捕获该事件并阻止提交表单。请注意,按Enter键提交是许多用户的预期行为!
#3
Make sure your text boxes are set to Multiline as TextMode, if not enter key is executing a command button causing the post back.
确保您的文本框设置为Multiline as TextMode,如果不是,则输入key正在执行命令按钮,导致回发。
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
If you do not want your text boxes multi-line, you can intercept the key press command in JavaScript to do nothing when entered is pressed.
如果您不希望文本框多行,则可以在JavaScript中截取按键命令,在按下输入时不执行任何操作。
This is an example how
这是一个例子
function hookUpToEnterKey()
{
// Hook up to read enter key
document.onkeydown = processEntryKey;
}
function processEntryKey(e)
{
if( !e )
{
if( window.event )
{
//Internet Explorer
e = window.event;
}
else
{
// Cannot get the even return.
return;
}
}
if( typeof( e.keyCode ) == 'number' )
{
//DOM
e = e.keyCode;
}
else
if( typeof( e.which ) == 'number' )
{
//NS 4 compatible
e = e.which;
}
else
if( typeof( e.charCode ) == 'number' )
{
//also NS 6+, Mozilla 0.9+
e = e.charCode;
}
else
{
//total failure, we have no way of obtaining the key code
return;
}
if(e == 13)
{
// Do nothing
return false;
}
}
#4
I had the exact same problem: I had a databound gridview, a textbox and a button. The textbox was used to filter the grid contents, as a search box. The button was used for a totally different action. Before putting the button, hitting the enter key on the textbox caused the grid to be filtered, which was expected behavior. But as soon I put the button, hitting enter while focus on the textbox caused the onclick event of the button to be called. I fixed it just by setting to true the autopostback property of the textbox. I don't understand why, but somehow it overrides the form submission behavior of the textbox. Hope it helps
我有完全相同的问题:我有一个数据绑定gridview,一个文本框和一个按钮。文本框用于过滤网格内容,作为搜索框。该按钮用于完全不同的动作。在按下按钮之前,按下文本框上的回车键会导致网格被过滤,这是预期的行为。但是一旦我按下按钮,在按下文本框时按下Enter键会导致按钮的onclick事件被调用。我通过将true设置为文本框的autopostback属性来修复它。我不明白为什么,但不知何故它覆盖了文本框的表单提交行为。希望能帮助到你
#5
Your browser is trying to be a nice guy and click the button for you, even if you didn't set it to be the "default" (it will "click" the first button in the form on enter).
您的浏览器试图成为一个好人,并为您单击按钮,即使您没有将其设置为“默认”(它将“点击”输入表单中的第一个按钮)。
Like David said, if you want the enter key to cause a line break in the textbox, it needs to be multi-line
就像David说的那样,如果你想让enter键在文本框中引起换行,那么它需要是多行的
#1
This is the default behaviour for most fields except for text areas.
这是除文本区域以外的大多数字段的默认行为。
To get around this you can call a javascript function before the form is submitted to check the keypress.
为了解决这个问题,您可以在提交表单之前调用javascript函数来检查按键。
<script type="text/javascript">
function allowSubmission() {
return !(window.event && window.event.keyCode == 13); }
</script>
Then the only way to submit the form is to actually hit submit. However as many people have mentioned, the enter key submitting a form is expected behaviour, so you can always alter the function to do basic validation on the fields, allowing the enter key to submit the form if all the required fields have been filled in.
那么提交表单的唯一方法就是实际点击提交。然而,正如许多人所提到的,提交表单的输入键是预期的行为,因此您可以随时更改函数以对字段进行基本验证,如果已填写所有必填字段,则允许输入键提交表单。
<script type="text/javascript">
function allowSubmission() {
return !(window.event && window.event.keyCode == 13) || validateInput(); }
</script>
Edit: You'd call this function on the OnClientClick method of your submit button. Something like:
编辑:您将在提交按钮的OnClientClick方法上调用此函数。就像是:
<asp:Button id="SubmitBtn" runat="server" OnClientClick="return allowSubmission()"/>
#2
This is true for any webform: if you press enter while an input field has focus, the form will be submitted (except textareas). To stop that, you'll need some JavaScript to capture that event and prevent the form being submitted. Be aware though, pressing enter to submit is expected behavior for many users!
对于任何webform都是如此:如果在输入字段具有焦点时按Enter键,则将提交表单(textareas除外)。要阻止这种情况,您需要一些JavaScript来捕获该事件并阻止提交表单。请注意,按Enter键提交是许多用户的预期行为!
#3
Make sure your text boxes are set to Multiline as TextMode, if not enter key is executing a command button causing the post back.
确保您的文本框设置为Multiline as TextMode,如果不是,则输入key正在执行命令按钮,导致回发。
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
If you do not want your text boxes multi-line, you can intercept the key press command in JavaScript to do nothing when entered is pressed.
如果您不希望文本框多行,则可以在JavaScript中截取按键命令,在按下输入时不执行任何操作。
This is an example how
这是一个例子
function hookUpToEnterKey()
{
// Hook up to read enter key
document.onkeydown = processEntryKey;
}
function processEntryKey(e)
{
if( !e )
{
if( window.event )
{
//Internet Explorer
e = window.event;
}
else
{
// Cannot get the even return.
return;
}
}
if( typeof( e.keyCode ) == 'number' )
{
//DOM
e = e.keyCode;
}
else
if( typeof( e.which ) == 'number' )
{
//NS 4 compatible
e = e.which;
}
else
if( typeof( e.charCode ) == 'number' )
{
//also NS 6+, Mozilla 0.9+
e = e.charCode;
}
else
{
//total failure, we have no way of obtaining the key code
return;
}
if(e == 13)
{
// Do nothing
return false;
}
}
#4
I had the exact same problem: I had a databound gridview, a textbox and a button. The textbox was used to filter the grid contents, as a search box. The button was used for a totally different action. Before putting the button, hitting the enter key on the textbox caused the grid to be filtered, which was expected behavior. But as soon I put the button, hitting enter while focus on the textbox caused the onclick event of the button to be called. I fixed it just by setting to true the autopostback property of the textbox. I don't understand why, but somehow it overrides the form submission behavior of the textbox. Hope it helps
我有完全相同的问题:我有一个数据绑定gridview,一个文本框和一个按钮。文本框用于过滤网格内容,作为搜索框。该按钮用于完全不同的动作。在按下按钮之前,按下文本框上的回车键会导致网格被过滤,这是预期的行为。但是一旦我按下按钮,在按下文本框时按下Enter键会导致按钮的onclick事件被调用。我通过将true设置为文本框的autopostback属性来修复它。我不明白为什么,但不知何故它覆盖了文本框的表单提交行为。希望能帮助到你
#5
Your browser is trying to be a nice guy and click the button for you, even if you didn't set it to be the "default" (it will "click" the first button in the form on enter).
您的浏览器试图成为一个好人,并为您单击按钮,即使您没有将其设置为“默认”(它将“点击”输入表单中的第一个按钮)。
Like David said, if you want the enter key to cause a line break in the textbox, it needs to be multi-line
就像David说的那样,如果你想让enter键在文本框中引起换行,那么它需要是多行的