How to capture enter event in asp.net

ASPNET_8
ASPNET_8 used Ask the Experts™
on
Hi

I want to capture the enter event in a multiline text box in asp.net 3.5 and want to store that data in SQL databasse,So that when i fetch that data in future it would be shown un the same format with proper spacing
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
<asp:TextBox ID="User_IDTextbox" Rows="3" Columns="50" TextMode="MultiLine" onkeypress="checkEnter(event)" runat="server"></asp:TextBox>

and ur JS function will be

function checkEnter(event)
{
     if (!event) event = window.event;
     key = event.keyCode ? event.keyCode : event.which;    

     if (key == 13)  //13 is keycode for enter key
     {
       //do stuff here.
     }
}

Author

Commented:
Can u pls explain me what does this code do??

This part

 if (!event) event = window.event;
     key = event.keyCode ? event.keyCode : event.which;  

Author

Commented:
How would i store the data in this Multiline text box with proper sppacing and retrieve it from there in the same format.??
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

window.event contains which key is pressed. In IE event.keyCode contains the value (While pressing enter the value will be 13). But in browsers such as firefox event.which will contain the value. This code to work with all browsers.
That javascript listens for the enter key which is only part of the solution. Inside the javascript that easwaranp provided, put code that adds "\r\n" to the textbox. This will give the newline in place of the enter.

If you just wanted the newline to write in the textbox from the codebehind you could use Environment.NewLine
VB.NET
txtTest.Text = "First Line " & Environment.NewLine & "Second Line"

C#
txtTest.Text = "First Line " + Environment.NewLine + "Second Line";

Author

Commented:
good

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial