将KeyPress事件添加到自定义文本框控件 [英] Adding KeyPress events to custom text box control

查看:107
本文介绍了将KeyPress事件添加到自定义文本框控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这是我在论坛中的第一个问题.

要求:我应该创建一个自定义文本框控件,该控件具有诸如Numeric,Alphabetic,AlphaNumeric之类的属性.假设已设置Numeric属性. .

我创建了一个自定义文本框控件,并添加了以下代码

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

public enum UserEntry
{
   AlphaNumeric,
   Numeric,
   Alphabetic,
}

public UserEntry userEntry;

private UserEntry ActionType
{

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




请帮我了解如何将按键事件与文件后面的代码关联(我正在创建一个新网站来对其进行测试)..实现这一目标..
现在还没有接触过Javscript..由于是一个新的Net框架,如果大家都可以帮助破解它,那就太好了..




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..

推荐答案

如果这是ASP.NET,您确实需要在客户端部分(在Web浏览器中)过滤事件.请参阅Jason C Daniels的答案,他提供了非常明确的动机.

我已经回答了类似的问题(找不到它...).使用Javascript,您可以过滤出所需数字格式所不允许的字符:

示例:

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:

<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>



这是纯HTML,只是为了演示脚本的工作原理.将其包装在ASP.NET中时,其工作方式相同.本示例过滤掉除十进制数字和退格键以外的所有内容.在其他情况下,您将包括-","e +-",字母等.您可以针对所有情况(一组允许的字符)开发通用过滤器,并将其作为另一个参数传递给与该函数类似的函数在我的示例中.



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.


为了处理网页中的按键,您必须编写一些javascript.话虽如此,您*可以*调用javascript处理程序中的代码背后的按键...,但是效率极低.

为什么?因为每次按键都会导致网络流量,并且因此引起非常明显的延迟.这意味着每个有效字符可能需要1/2至3秒(或更长时间!)出现在文本框中.在实际应用中,这样的性能特征通常是不可接受的.

这是一篇文章的链接,该文章讨论了如何附加到javascript中的onKeyPress事件.

捕获Enter键以引起按钮单击 [ ASP.NET中的脚本回调 [^ ]
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[^]


这篇关于将KeyPress事件添加到自定义文本框控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆