Click here to Skip to main content
15,914,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My code is as follows, if i use the same code wihtout master page, it is working, but with master page,
it is not calling keypress/keydown event.

XML
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
    CodeFile="Default.aspx.vb" Inherits="_Default" %>


<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script language="javascript" type="text/jscript" >
        function test() {
        alert("hai");
    }
    </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
   <asp:TextBox ID="emptxtbox" Text="" MaxLength="4" runat="server"
                                                    onkeydown="return test();" Width="200px" Height="50px"
                                                    Font-Size="XX-Large" />
</asp:Content>
Posted
Updated 24-May-12 21:49pm
v3

 
Share this answer
 
try this , it works in both:
C#
function isNumericKeyStroke(evt) {
           var returnValue = false;
           var charCode = (window.Event) ? evt.which : evt.keyCode
           if (((charCode >= 48) && (charCode <= 57)) || // All numerics
          (charCode == 8) ||     // Backspace
          (charCode == 13))     // Carriage Return
           {
               returnValue = true;
           }
          return returnValue;
       }
 
Share this answer
 
v2
Comments
Bhargav1985 25-May-12 3:22am    
Hi thanx for solution, but my problem is with using master page.
My code as follows

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script language="javascript" type="text/jscript" >
function isNumericKeyStroke(evt) {
alert("key pressed !");
var returnValue = false;
var returnValue = false;
var charCode = (window.Event) ? evt.which : evt.keyCode
if (((charCode >= 48) && (charCode <= 57)) || // All numerics
(charCode == 8) || // Backspace
(charCode == 13)) // Carriage Return
{
returnValue = true;
}
return returnValue;
</script>



<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:TextBox runat="server" ID="txtCredit"
Width="80" onkeypress= "return isNumericKeyStroke(event)" onkeydown="return isNumericKeyStroke(event)"/>
It is a known issue and multiple discussions[^] can be found on internet.

FF doesn't use window.event for keypress, but expects the event to be passed as a parameter to the function handling it
Look 'Fang' answer here for similar discussion on how to handle it: window.event.keycode 13 won't work in Firefox[^]

Another link that will help: http://www.ryancooper.com/resources/keycode.asp[^]
 
Share this answer
 
sometimes KeyCode does not work with firefox ,use window.event with if condition to get rid of error in Firefox.
such as :
HTML
var charCode = (window.Event) ? evt.which : evt.keyCode
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900