Unable to find textbox keypress event in ASP.NET

I want to trap the keypress event of a textbox in a web page developed in ASP.NET. How will I do it.
 
sam78_myAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
JNSTAUBConnect With a Mentor Commented:
it may be:
<asp:TextBox onTextChanged="myTextHandler" runat="server" />
or a client script
post back at each keystrokes is bandwidth consumming!!!
0
 
yourounConnect With a Mentor Commented:
as above, you could add a server event to detect key press, wouldn't do that, cuz it's bandwith consuming...
It's better to add client side code, in order for it to work though, you have to add the client event in your server script:

private void Page_Load(object sender, System.Eventargs e)
{
   textBox1.Attributes.Add("onkeypress", "javascriptFunctionHere()");
}

and in your client page add the function:

<script language=javascript>
   function javascriptFunctionHere()
   {
      alert('do something here, like show this message');
   }
</script>

client side script works most of the time, can't see of any reason why to do a postBack on every keypress ...
0
 
YZlatConnect With a Mentor Commented:
Sub Page_Load( ByVal sender As System.Object, ByVal e As System.EventArgs)
            txtTextbox.Attributes.Add("onKeyPress", _
                  "JavaScript:HandleOnKeyPress()")
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.