Avatar of zintech
zintech
 asked on

How to register KeyPressed event in C# ASP.NET

I have a methord called

 TextBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs 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.
.NET ProgrammingC#ASP.NET

Avatar of undefined
Last Comment
Mlanda T

8/22/2022 - Mon
HainKurt

is this web application? do you want to run a javascript function on client, not server, correct?

if yes,

TextBox1.attributes("onKeyPress","js_KeyPressed(event, this)")

and add a js function

<script>
function js_KeyPressed(event, sender){
  alert(event.key); alert(sender.id);...
}
</script>
HainKurt

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)">

Open in new window

zintech

ASKER
So you are saying I need to register the control on the ASP.NET side then in the HTML, okay
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
HainKurt

when you use this

<asp:Textbox id=test runat=server>
...
TextBox1.attributes("onKeyPress","js_KeyPressed(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

Mlanda T

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.KeyEventArgs e)" method. First try to do it all in javascript, and if thats not possible, consider asyncronous javascript calls to the server....
HainKurt

the samples I posted are all on client side, I dont even think of server side firing for each key event...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Mlanda T

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question