Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: 'keyCode' is null or not an object

Tags:

javascript

c#

I am getting this error when i run this function

<script language="javascript" type="text/javascript">
        //function for check digit
    function check_no(e)
         {
             if (!((e.keyCode >= 48) && (e.keyCode <= 53)))
             {
                 alert("Solo valores entre 0 y 5 pueden ser ingresados");
                 e.keyCode = 0;
             }
         }
</script>

I call the function in my load page in c#

foreach (GridViewRow grdrow in DGPlanilla.Rows)
                {
                    TextBox tb1 = (TextBox)grdrow.FindControl("TextBox1");
                    if (tb1 != null)
                    {
                        tb1.Attributes.Add("Onkeypress", "check_no()");
                    }

                }

1 Answers

The problem with the snippet you've pasted, is that the javascript function expects a single argument e, which you've not supplied. Your e actually needs to be the window.event property for this call to work.

You have two options. Either rewrite your function to be:

function check_no() { 
  if (!((window.event.keyCode >= 48) ... some other stuff
}

OR, rewrite the calling code to be

foreach (GridViewRow grdrow in DGPlanilla.Rows) 
{ 
  TextBox tb1 = (TextBox)grdrow.FindControl("TextBox1"); 
  if (tb1 != null) { tb1.Attributes.Add("Onkeypress", "check_no(window.event)"); 
}
like image 64
Rob Levine Avatar answered Jul 20 '26 13:07

Rob Levine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!