?
Solved

capture keypress event and call another function

Posted on 2005-03-15
20
Medium Priority
?
814 Views
Last Modified: 2012-08-13
Hi All:

I want to capture keypress event and call another function in the keypress function. I dont want to do that in javascript I want to do that in code behind for my aspx page.
How do i do it?

This is what i have...?
<script language="JavaScript">
function handler(e,r) {
if (document.all) {
        e = window.event;
    }
   
    var key;

    if (document.layers)
        key = e.which;
    if (document.all)
        key = e.keyCode

    var chr = String.fromCharCode(key);

   // Here i want to call another function like this.  Getvalues(chr);
    //alert('Character representation of pressed was ' + chr);
 
}
</script>

This is javascript code to capture keypress event...But i want to pass chr variable another function which will connect to database.Hence i want all this code is my codebhind file.

Please help me.
Ashwin.

0
Comment
Question by:Ashwin_shastry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +2
20 Comments
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13545521
do u really need that?? it would too much of a processing for ur browser as well as server .. imagine a submit to the server for every character entered .. if u could use the really neeed that then set the "autopostback" property of the textbox to true and then add ur code in the "TextChanged" event of the textbox .. even this i think would do a postback when the whole text is changed and not for each alphabet ... but still if you can tell us y there is such a requirement, maybe we could think of some other alternative ...
0
 

Author Comment

by:Ashwin_shastry
ID: 13545537
Yep i know...autopostback works...but I really need this on key press..
0
 
LVL 10

Expert Comment

by:EBatista
ID: 13546999
add onclick attribute to the text box
theBox.Attributes("OnKeypress") = "javascript: __doPostback('theBox','');"
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 10

Expert Comment

by:EBatista
ID: 13547035
or you can place a button in your page and do a click on it via javascript:
theBox.Attributes("OnKeypress") = "javascript:document.Form1.btnSearch.click();"
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 13547179
Here's one way of accessing server side functions from JavaScript & passing some values along..

In your JavaScript function where ever you want to call some server side function add this line:

JavaScript:
-------------
 __doPostBack('CallFunction','123');

& Now in your PageLoad write following code:
C#
---------
if (Request.Form["__EVENTTARGET"] == "CallFunction")
{
   YourFunction( Request.Form["__EVENTARGUMENT"].ToString() ); // this will pass "123" to the function.
}

VB
---------
Request.Form("__EVENTTARGET")


-tushar
0
 

Author Comment

by:Ashwin_shastry
ID: 13548177
I dont want to happen postback for my page...what to do...
0
 
LVL 10

Expert Comment

by:EBatista
ID: 13548273
Ashwin_shastry, you said want to do that in code behind, now, the only way to do that is doing a postback, please clarify.
0
 

Author Comment

by:Ashwin_shastry
ID: 13548401
See if i do the postback...the values which i m passing becomes empty after postback. I want to retain the values which i pass to codebehind even after postback.
0
 
LVL 10

Expert Comment

by:EBatista
ID: 13548620
well save the viewstate of the control and set it again after postback.
anyway i would like to see your code
0
 
LVL 11

Expert Comment

by:jatinderalagh
ID: 13552204
You can try to use xml HTTP callbacks.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13552605
if ur browser is IE and ur page would not be used in any other browser, use the XMLHTTP object .. create a aspx page which will accept the last typed character in a querystring (say ProcessCharacter.aspx) .. process it as u want ..

in the load event add this code
UrTextbox.Attributes("OnKeypress") = "vbscript:SendValue"

Then create a client-side vbscript procedure in the aspx page
Sub SendValue
      Set lobjXMLHTTP = createObject(Microsoft.XMLHTTP)
      lstrURL = "http://urserver/ProcessCharacter.aspx?LastCharacter" & window.event.keycode
      lobjXMLHTTP.open "GET", lstrURL, false
      lobjXMLHTTP.send
En Sub
0
 

Author Comment

by:Ashwin_shastry
ID: 13555309
Hi Rejojohny...I am using C#....how to use vb script there...
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13555389
what i have given is a client side script and assuming ur browser is IE( as mentioned XMLHTTP will work only in IE), i have given the solution in vbscript ..
0
 

Author Comment

by:Ashwin_shastry
ID: 13556139
Can I have a timer in webapplication instead and check the textbox for keystrokes every one second...and take the values
0
 
LVL 26

Accepted Solution

by:
Rejojohny earned 375 total points
ID: 13562556
u have a look at settimeout function in IE browsers .. but then what u r talking about wil not be a solution for u .. even if u use a timer how will check for keystrokes ... even if r able to check for the keystrokes how will send the character to the server which is what u require
0
 
LVL 11

Assisted Solution

by:jatinderalagh
jatinderalagh earned 375 total points
ID: 13562735
That's right. Using XML HTTp you can refresh the particular part of page rather then doing the complete postback.

How to use this you can find in MSDN
http://msdn.microsoft.com/msdnmag/issues/04/08/CuttingEdge/default.aspx

And Netscape also has XML Http support you need to check the JavaScript
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13761035
Please do have a look at my comments ..
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question