I have 2 textboxes and a submit button inside a .net composite server control. I tried to only postback when submit button is clicked. I set autopostback = false for both textboxes. But either one still trigge开发者_Python百科r submit button's onclick event by hitting "Enter" inside textbox. How can I stop postback by enter key?
The browser will automatically click the submit button if you press Enter inside a text box.
You can suppress this behavior using Javascript.
To block the enter key from submitting on a single textbox try this bit of code from http://forums.asp.net/t/1227095.aspx
First add this function to your page javascript
<script type="text/javascript">
function noenter() {
return !(window.event && window.event.keyCode == 13); }
</script>
and then, on page_load addthe following for each textbox on the page that you wish to block the enter key:
txtUsername.Attributes.Add("onkeypress", "return noenter()");
精彩评论