Link to home
Start Free TrialLog in
Avatar of Ashwin_shastry
Ashwin_shastry

asked on

capture keypress event and call another function

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.

Avatar of Rejojohny
Rejojohny
Flag of United States of America image

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 ...
Avatar of Ashwin_shastry
Ashwin_shastry

ASKER

Yep i know...autopostback works...but I really need this on key press..
add onclick attribute to the text box
theBox.Attributes("OnKeypress") = "javascript: __doPostback('theBox','');"
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();"
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
I dont want to happen postback for my page...what to do...
Ashwin_shastry, you said want to do that in code behind, now, the only way to do that is doing a postback, please clarify.
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.
well save the viewstate of the control and set it again after postback.
anyway i would like to see your code
You can try to use xml HTTP callbacks.
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
Hi Rejojohny...I am using C#....how to use vb script there...
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 ..
Can I have a timer in webapplication instead and check the textbox for keystrokes every one second...and take the values
ASKER CERTIFIED SOLUTION
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please do have a look at my comments ..