Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
This is my first question in the forum.

Requirement : I am supposed to create a custom text box control which has properties such as Numeric, Alphabetic, AlphaNumeric.Suppose Numeric property is set .. user should be able to enter only Numbers in text box, Similarly the functionality corressponds to Alphabetic, AlphaNumeric.

I have a created Custom Text Box Control and have added the following code
C#
public enum UserEntry
{
   AlphaNumeric,
   Numeric,
   Alphabetic,
}

public UserEntry userEntry;

private UserEntry ActionType
{

    get
    {
         return userEntry;
    }
    set
    {
         userEntry = value;
    }
}



Please help me out how i can associate keypress events in the code behind file (a am creating a new website just to test this)..to achieve this..
Am not exposed to Javscript either yet.. Since am fresh ,Net framework.. it would be really nice if u all can help in cracking this..
Posted
Updated 16-Jan-11 17:09pm
v4
Comments
Sergey Alexandrovich Kryukov 16-Jan-11 22:22pm    
Edited the answer. What this: Grammar Police: http://www.youtube.com/watch?v=u9_kahA_wQo

If this is ASP.NET you really need to filter events in the client part (in the Web browser). Please see the Answer by Jason C Daniels who provided very clear motivation.

I already answered similar question (cannot find it...). With Javascript, you can filter out characters not allowed in the numeric format you need:

Example:

XML
<html>
    <head>
        <script type="text/javascript"><!--
            function filterDigits(eventInstance) {
                eventInstance = eventInstance || window.event;
                    key = eventInstance.keyCode
                    || eventInstance.which;
                if ((47 < key) && (key < 58)
                    || key == 8) {
                        return true;
                } else {
                        if (eventInstance.preventDefault)
                            eventInstance.preventDefault();
                        eventInstance.returnValue = false;
                        return false;
                    } //if
            } //filterDigits
        --></script>
    </head>
<body>
<input type="text" onkeypress="filterDigits(event)"/>
</body>
</html>


This is pure HTML, just to show how the script works. When this is wrapped in ASP.NET stuff, it works the same way. This example filters out everything except decimal digits and backspace. In other cases, you will include "-", "e+-", letters, etc. You can develop a universal filter for all cases (a set of allowed characters) and pass it as yet another parameter to a function similar to the one in my sample.
 
Share this answer
 
v5
Comments
Jason C Daniels 16-Jan-11 23:53pm    
Good example man!
Manfred Rudolf Bihy 17-Jan-11 1:33am    
Good call! 5+
In order to process key-strokes in a webpage, you MUST write some javascript. With that said, you *could* call into the codebehind in the javascript handler for the keypress... but it will be horribly inefficient.

Why? Because you will cause network traffic, and therefore a very noticable delay, with every keypress. This means that it may take 1/2 to three seconds (or more!) for each valid character to show up in the text box. Such performance characteristics are usually unacceptable in a real-world application.

Here is a link to an article which discusses attaching to the onKeyPress event in javascript.

Capturing the Enter key to cause a button click[^]


It only processes the enter key, but hooks things up much like you have indicated you want.

To learn more about how to perform a callback to the serverside, read this article: Script Callbacks in ASP.NET[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Jan-11 23:08pm    
Jason, thank you for this answer (my 5), it makes things much more clear.
Jason C Daniels 16-Jan-11 23:50pm    
It was my pleasure.
Sergey Alexandrovich Kryukov 17-Jan-11 0:58am    
Jason, I had to update my answer to reference your important explanation. Thank you again -- SA
Manfred Rudolf Bihy 17-Jan-11 1:34am    
Good advice! 5+

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