ASP.NET - HOW TO FIRE A JAVAFUNCTION ON SPACE BAR HIT

Asked By Vidyasagar Patil on 14-Feb-12 12:32 AM


 HI friends

I am having a problem for the past few days,there are almost more than 50 pages in my project and on each page ther is a button to save or either to update and i have written a javascript function to validate the fields in respective pages
It works fine when i hit enter by taking the focus on the button.....but if i take focus on the button and i press spacebar...
the javascript is not fired the codebehind logic is fired and hence it gives me an error "Object reference not set to an instance of an object"


 I want to fire the javascript even if i hit space bar and i want to do it for all the pages in my project so i need to make it generic...thats why i need to write it on master page.....

Please paste ur code here


Thanks in advanced
Web Star replied to Vidyasagar Patil on 14-Feb-12 12:40 AM
you need to check keycode for space bar which is 32, So you can use this below javascript and call  allowSubmission on button click or keypress event of textbox whatever you needed

<script type="text/javascript">
  function allowSubmission() {
  return !(window.event && window.event.keyCode == 32) || validateInput(); }
</script>

hope this helps your
Chintan Vaghela replied to Vidyasagar Patil on 14-Feb-12 12:44 AM
Hello

Try as following way

$(document).keydown(
function(e)
{
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
if (key == 32)
alert("space bar is pressed");
}
);


hope this helpful
Somesh Yadav replied to Vidyasagar Patil on 14-Feb-12 01:51 AM
Hi,

First: there's a problem with your HTML. You're using the short syntax for the A -- meaning that the text isn't actually part of the A content. Many browsers don't like the short syntax for A tags anyway -- not your fault, but it's our job to be more tolerant than browsers are.

Here's the corrected version:

<a tabindex="7" style="cursor: pointer;" class="button" id="saveTocAddNew" onClick="saveTdsAddNew();"><span>Save & Add Another</span></a>

Depending on the browser and DOCTYPE, it it significant whether the event name is all lowercase or not. Just something to look for.

Finally, by default your onclick event will only fire for clicks and Enter -- not for spacebar. Many browsers treat the spacebar as a pagedown thing. If you are sure you want to capture spacebar events on this A and treat them like Enter, you'll need to define a keypress event that looks for spacebars. Like so:

function addNewKeys(event) {
   
if(!event) var event = window.event; // cross-browser shenanigans
   
if(event.keyCode === 32) { // this is the spacebar
        saveTdsAddNew
(event);
   
}
   
return true; // treat all other keys normally;
}

(Remember to bind this new function to the onkeypress of that A.)

Note that I'm passing the event to the saveIdsAddNew function. That's because I think event handlers normally receive the event object as their argument, so this preserves the existing pattern. From the event, you can retrieve the element that was clicked/keyed-on, etc.


Hope it helps you.
kalpana aparnathi replied to Vidyasagar Patil on 14-Feb-12 05:28 AM
hi

Use below code for solution:

function addNewKeys(event) 
{

   
if(!event) var event = window.event; // cross-browser shenanigans
   
if(event.keyCode === 32) { // this is the spacebar
        saveTdsAddNew
(event);
   
}
   
return true; // treat all other keys normally;
}

<a tabindex="7" style="cursor: pointer;" class="button" id="saveTocAddNew" onClick="saveTdsAddNew();"><span>Space bar Hit</span></a>

Error:
One common mistayke is when working with Codebehind Classes
You need to declare EVERY control you want to interact with in the codebehind class
Cause 2: Bad scoping!
Bad inits/constructs

Regards,