zintech
asked on
How to register KeyPressed event in C# ASP.NET
I have a methord called
TextBox1_KeyUp(object sender, System.Windows.Forms.KeyEv entArgs e)
That I would like to register and have fire on the Textbox named TextBox1 when the user presses a ley up but I do not know how to register it or use it.
TextBox1_KeyUp(object sender, System.Windows.Forms.KeyEv
That I would like to register and have fire on the Textbox named TextBox1 when the user presses a ley up but I do not know how to register it or use it.
oops, it should be keyCode... your rendered page should look like this
<script>
function js_KeyPressed(event, sender){
alert(event.keyCode); alert(sender.id);
}
</script>
<input id=txtTest onKeyPress="js_KeyPressed(event, this)">
ASKER
So you are saying I need to register the control on the ASP.NET side then in the HTML, okay
when you use this
<asp:Textbox id=test runat=server>
...
TextBox1.attributes("onKey Press","js _KeyPresse d(event, this)")
it is rendered as
<input id=xxx_xxx_xxxx_test onKeyPress="js_KeyPressed( event, this)">
and on the page if you have that js function it will be called whenever you press a key inside that box
<asp:Textbox id=test runat=server>
...
TextBox1.attributes("onKey
it is rendered as
<input id=xxx_xxx_xxxx_test onKeyPress="js_KeyPressed(
and on the page if you have that js function it will be called whenever you press a key inside that box
It's not good practice to fire up server side code like that in a web application - at least not for each keypress. It's fine for desktop applications. What are you trying to achieve?
It is best practice to process the key press in client side script, and I prefer using jQuery (http://dotnetstars.blogspot.com/2011/02/adding-keypress-event-to-all-textboxes.html) for this sort of thing because it is cleaner and peformance-wise, will remain fast. The best guidance will really depend on what you are doing in the " TextBox1_KeyUp(object sender, System.Windows.Forms.KeyEv entArgs e)" method. First try to do it all in javascript, and if thats not possible, consider asyncronous javascript calls to the server....
It is best practice to process the key press in client side script, and I prefer using jQuery (http://dotnetstars.blogspot.com/2011/02/adding-keypress-event-to-all-textboxes.html) for this sort of thing because it is cleaner and peformance-wise, will remain fast. The best guidance will really depend on what you are doing in the " TextBox1_KeyUp(object sender, System.Windows.Forms.KeyEv
the samples I posted are all on client side, I dont even think of server side firing for each key event...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if yes,
TextBox1.attributes("onKey
and add a js function
<script>
function js_KeyPressed(event, sender){
alert(event.key); alert(sender.id);...
}
</script>