Atlas学习手记(17):使用FilteredTextBox过滤字符

FilteredTextBox Extender可以过滤TextBox中用户输入的不合法字符,使用JavaScript和验证控件来检测时一般都是在用户输入之后再去判断是否合法,使用FilteredTextBox则可以直接阻止不让用户输入不合法的字符,也避免了将数据传入到服务端的时候再去验证。

主要内容

1FilteredTextBox介绍

2.完整示例

一.FilteredTextBox介绍

FilteredTextBox Extender可以过滤TextBox中用户输入的不合法字符,使用JavaScript和验证控件来检测时一般都是在用户输入之后再去判断是否合法,使用FilteredTextBox则可以直接阻止不让用户输入不合法的字符,也避免了将数据传入到服务端的时候再去验证。示例代码如下:

<atlasToolkit:FilteredTextBoxExtender ID="ftbe" runat="server">

    
<atlasToolkit:FilteredTextBoxProperties 

        
TargetControlID="TextBox3" 

        FilterType
="Custom, Numbers"

        ValidChars
="+-=/*()." />

</atlasToolkit:FilteredTextBoxExtender>

它的属性如下:

属性

描述

TargetControlID

要进行过滤的目标TextBoxID

FilterType

字符过滤类型,提供的有如下四种:NumbersLowercaseLettersUppercaseLettersCustom他们之间可以同时指定多个类型,如:

FilterType="Custom, Numbers"

ValidChars

FilterTypeCustom时允许输入的字符,否则将被忽略

ValidChars="+-=/*()."

二.完整示例

FilteredTextBox Extender的使用非常简单,下面看一个完整的示例。新建一个WebSite后,在ASPX页面的顶部加入:

<%@ Register Assembly="AtlasControlToolkit" 

            Namespace
="AtlasControlToolkit" 

            TagPrefix
="atlasToolkit" 
%>

在页面上添加三个TextBox,分别做三种不同的过滤:

<div>

    
<table border="0">

        
<tr>

            
<td>

                只能输入数字:
</td>

            
<td>

                
<asp:TextBox ID="TextBox1" runat="server" />

            
</td>

        
</tr>

        
<tr>

            
<td>

                只能输入小写字母:
</td>

            
<td>

                
<asp:TextBox ID="TextBox2" runat="server" /></td>

        
</tr>

        
<tr>

            
<td>

                只能输入符号(+,-,*,/,=,.)和数字:
</td>

            
<td>

                
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>

        
</tr>

    
</table>
</div>

添加FilteredTextBoxExtender,对每一个TextBox都要添加FilteredTextBoxProperties,设置参数如下:

<atlasToolkit:FilteredTextBoxExtender ID="ftbe" runat="server">

    
<atlasToolkit:FilteredTextBoxProperties 

        
TargetControlID="TextBox1" 

        FilterType
="Numbers"/>

    
<atlasToolkit:FilteredTextBoxProperties 

        
TargetControlID="TextBox2" 

        FilterType
="LowercaseLetters" />

    
<atlasToolkit:FilteredTextBoxProperties 

        
TargetControlID="TextBox3" 

        FilterType
="Custom, Numbers"

        ValidChars
="+-=/*()." />

</atlasToolkit:FilteredTextBoxExtender>

运行后如下:

输入字符,可以看到在第一个TextBox中不可以输入字母和符号,第二个TextBox只可以输入小写字母:

完整的示例下载

posted on   秋天  阅读(286)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2008年3月 >
24 25 26 27 28 29 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示