开发者

Textbox always trigger postback event with autopostback = false inside composite control

开发者 https://www.devze.com 2023-01-02 13:17 出处:网络
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 sti

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()");
0

精彩评论

暂无评论...
验证码 换一张
取 消