[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 15792
  • Last Modified:

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.
 
0
sam78_my
Asked:
sam78_my
3 Solutions
 
JNSTAUBCommented:
it may be:
<asp:TextBox onTextChanged="myTextHandler" runat="server" />
or a client script
post back at each keystrokes is bandwidth consumming!!!
0
 
yourounCommented:
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
 
YZlatCommented:
Sub Page_Load( ByVal sender As System.Object, ByVal e As System.EventArgs)
            txtTextbox.Attributes.Add("onKeyPress", _
                  "JavaScript:HandleOnKeyPress()")
End Sub
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now