Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trapping keyboard events in ASP.NET with C#

Status
Not open for further replies.

wbochar

IS-IT--Management
Mar 14, 2003
72
US
Is it possible to trap keyboard events like CTRL-S in an explorer webpage?

Does anyone have any code snippets or a pointer in the right direction?

I've seen the AccessKey Property, but it only seems to navigate to the point where that control is and give it focus. I want to be able to use things like CTRL-S to save the form to DB (post).

wbochar
 
I honestly haven't investigated ASP.NET's ability in this regard. But this is because I am a big proponent of coding client-side in DHTML, JavaScript, and CSS, and using server-side for business logic and data access.

So, in JavaScript, you can add the "onkeypress" handler to most controls, and pass that control to a JavaScript function. JavaScript also provides an "event" object, which contains a wealth of information, including key modifiers, right-click vs left-click, and even the x,y coordinates of the mouse.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
I would look to Javascript...I use an event trap to not allow users to Right Click...works good...and I probably got the code here at Tek-Tips
 
Can you use a Javascript Function to trigger a c# method?
 
Not directly. This is a common misunderstanding with ASP.NET. This is a very brief and broad description of what goes on with ASP.NET:

1) The user browses to an ASP.NET page.

2) The IIS server "compiles" and/or runs the ASP.NET page, which renders HTML and JavaScript to the browser.

3) At this point, the user is seeing basic HTML. The page contains some scripts. The only way for control to return back to the server, is for the page's form to be sumbitted.

4) The user fills out the form, and clicks a button or link (or "whatever").

5) A Page Script runs that determines what was clicked, etc., and builds all of that information into a hidden form variable (the infamous "Viewstate").

6) A "_doPostBack" script runs, which does some housekeeping, and then submits the form.

7) Back on the server, the posted form data is unpackaged and turned into ASP.NET events, which are delegated to their assigned methods.

With that understood, you realize that the only thing, in essence, that a user/browser can do is SUBMIT THE FORM.

One technique I often use is to create hidden textboxes. I assign JavaScript functions to various controls, for client-side validation, etc. When those functions run, I set the relevant textbox values, and submit the form.

Then, in the server-side Page_Load method, I interrogate those values, and call the appropriate functions.

So the direct answer to your question is "NO. JavaScript, as with any other client-side code, can only submit the form."

The longer-winded answer is "ASP.NET doesn't "call" server-side methods, either. Mimic what it does, by setting your own values, and submitting the form."



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top