Click here to Skip to main content
15,905,782 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to create KeyPress Event in asp.net page, where the event call is coded in C# rather than in JavaScript? Thanks.
Posted

ASP.NET code runs entirely server side. It will never know if the client hit a key or not in the browser.

You have no choice but to handle whatever your requirement is in Javascript on the client side.
 
Share this answer
 
Comments
s yu 13-Aug-15 15:19pm    
Got it. Will use JS. Thanks.
If you are looking for something like below,
Create a JS function and call that on ASP control onkeypress event
C#
function LettersWithSpaceOnly(evt) {
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
    ((evt.which) ? evt.which : 0));
        if (charCode > 32 && (charCode < 65 || charCode > 90) &&
        (charCode < 97 || charCode > 122)) {
            return false;
        }
        return true;
    }



<asp:textbox id="txtFN" runat="server" width="100%" maxlength="25" onkeypress="return LettersWithSpaceOnly(event);" xmlns:asp="#unknown">


Happy Coding!!!
 
Share this answer
 

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



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