Click here to Skip to main content
15,881,630 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,

I have a textbox and calendar extender control.
When I select the textbox, the calendar control is displaying properly. But, if I press enter button on the keyboard whenever the cursor is in the textbox the page will postback and signout.

If anybody knows, please help me.

thanking you.
Posted
Updated 19-Nov-10 23:13pm
v2
Comments
Dalek Dave 20-Nov-10 5:13am    
Edited for Grammar and Syntax.

Hi Sathish!

Try this:

XML
<asp:TextBox ID="TextBox1" runat="server"

   onkeydown = "return (event.keyCode!=13);" >

</asp:TextBox>


This solution was provided by Mudassar Khan on www.aspsnippets.com
The effect is that when being in the textbox and hitting the enter key, it will be ignored because the function returns false.

Cheers

Manfred
 
Share this answer
 
v3
Comments
Dalek Dave 20-Nov-10 5:14am    
Excellent answer!
Manfred Rudolf Bihy 20-Nov-10 5:31am    
Thank you Dave!
Ankur\m/ 20-Nov-10 5:39am    
5! I will just add a little more explanation.
Pressing an enter key when the focus is on a textbox submits the page. This is by design.
The key code for enter key is 13. So workaround given in the answer would always return a key code not equal to the value of Enter key code and thus it is like enter key was never pressed inside the textbox.
Manfred Rudolf Bihy 20-Nov-10 6:39am    
Hi Ankur, I'm not quite sure how to interpret you wording "would always return a key code ...". The workaround does it's magic by returning false in case the enter key was pressed. Returning false in a javascript event handler effectively stops the event from bubbling up the chain. (If that's what you meant, I totally concurr) ;)
Thanks for the five, though!
Ankur\m/ 20-Nov-10 6:51am    
Read it like this: would always return a key code not equal to the value of Enter key code.
In other words, it means the same as you just said. Sorry for the confusion and thanks for a better interpretation.
BTW, I added an explanation because this question is asked many a times and most of the people don't know why it happens. Understanding the reason behind a particular behaviour helps developing a better system. :)
You may have several submit buttons on you form.

When pressing enter key, it might simulate the wrong button.

Try to define the proper button for the default submit button on your
Page_OnLoad event:
this.form1.DefaultButton=btnMyDefaultButton.ClientID;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900