WebContorl示例--ASCII TextBox

AsciiTextBox代码
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Text;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;

[assembly: WebResource(
" WebUIControl.Js.CheckAsciiTextBox.js " " application/x-javascript " )]
namespace  WebUIControl
{
    [DefaultProperty(
" Text " )]
    [ToolboxData(
" <{0}:AsciiTextBox runat=server></{0}:AsciiTextBox> " )]
    
public   class  AsciiTextBox : TextBox
    {
        
protected   override   void  OnInit(EventArgs e)
        {
            
base .OnInit(e);
            
if  ( ! Page.ClientScript.IsClientScriptIncludeRegistered(Page.GetType(),  " AsciiTextBox " ))
            {
                
string  webUrl  =  Page.ClientScript.GetWebResourceUrl(GetType(),  " WebUIControl.Js.CheckAsciiTextBox.js " );
                Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), 
" CheckAsciiTextBox " , webUrl);
            }

        }

 

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

       //禁用输入法
            this.Attributes.Add("style""ime-mode:disabled");

       //在TextBox失去焦点时触发事件
            
this .Attributes.Add( " onblur " " return CheckAsciiTextBox(' "   +   this .ID  +   " ') " );
        }


    }
}

 

 

 

js代码
// 作为嵌入的资源
function  CheckAsciiTextBox(id)
{
    
var  tmp  =   "" ;
    //按ID取得页面控件
    
var  input  =  document.getElementById(id);
    
for ( var  i = 0 ;i < input.value.length;i ++ )
    {
        //判断字符串,注意charCodeAt和charAt的区别
        
if (input.value.charCodeAt(i) > 255 )
        {
            tmp 
+=  input.value.charAt(i);
        }
    }
    
    
if (tmp  !=   "" )
    {
        //提示用户
        alert(
" Contains the following illegal characters\ ""  + tmp +  " \ "" );
        //设置控件获取焦点
        input.focus();
    }
    
    
return   false ;
}


 

 

你可能感兴趣的:(ASCII)