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.
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.
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 ...
ASKER
Yep i know...autopostback works...but I really need this on key press..
add onclick attribute to the text box
theBox.Attributes("OnKeypr ess") = "javascript: __doPostback('theBox',''); "
theBox.Attributes("OnKeypr
or you can place a button in your page and do a click on it via javascript:
theBox.Attributes("OnKeypr ess") = "javascript:document.Form1 .btnSearch .click();"
theBox.Attributes("OnKeypr
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["__EVENTTARG ET"] == "CallFunction")
{
YourFunction( Request.Form["__EVENTARGUM ENT"].ToSt ring() ); // this will pass "123" to the function.
}
VB
---------
Request.Form("__EVENTTARGE T")
-tushar
In your JavaScript function where ever you want to call some server side function add this line:
JavaScript:
-------------
__doPostBack('CallFunction
& Now in your PageLoad write following code:
C#
---------
if (Request.Form["__EVENTTARG
{
YourFunction( Request.Form["__EVENTARGUM
}
VB
---------
Request.Form("__EVENTTARGE
-tushar
ASKER
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.
ASKER
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
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("OnKe ypress") = "vbscript:SendValue"
Then create a client-side vbscript procedure in the aspx page
Sub SendValue
Set lobjXMLHTTP = createObject(Microsoft.XML HTTP)
lstrURL = "http://urserver/ProcessCharacter.aspx?LastCharacter" & window.event.keycode
lobjXMLHTTP.open "GET", lstrURL, false
lobjXMLHTTP.send
En Sub
in the load event add this code
UrTextbox.Attributes("OnKe
Then create a client-side vbscript procedure in the aspx page
Sub SendValue
Set lobjXMLHTTP = createObject(Microsoft.XML
lstrURL = "http://urserver/ProcessCharacter.aspx?LastCharacter" & window.event.keycode
lobjXMLHTTP.open "GET", lstrURL, false
lobjXMLHTTP.send
En Sub
ASKER
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 ..
ASKER
Can I have a timer in webapplication instead and check the textbox for keystrokes every one second...and take the values
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Please do have a look at my comments ..