最近做的一个项目中要添加一个小功能,就是在客户输入药品名称的时候能自动产生药材名的拼音首字符及药材名称第一个字的笔画数,在以后的药材检索中中用到。

   为了方便药材信息录入我想到用AJAX解决,汉字拼音和笔画可以用微软提供的Visual Studio International Pack类库。

1、引用ChnCharInfo.dll、ChnCharInfoResource.dll到WEB项目中


2、页面引用Jquery脚本库并加一个JS函数:

</script>

<script language="javascript" type="text/javascript"> 

<!--
function ChnCharInfo(o){
    $.getJSON(
        
"ChnCharInfo.ashx?s=" + encodeURI(o.value),
        
function(json){
            
try{
                 $(
"#medspell").value = json.pingying;  //拼音
                 $(
"#medpen").value = json.bihua;  //笔画
            }
catch(e){}
        }
    );
}
// --> 
</

 

3、TextBox 加上 onblur 事件: TextBox1.Attributes.Add("onblur", "ChnCharInfo(this)");

4、新增一个 ChnCharInfo.ashx 文件:

 

 ChnCharInfo : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {

        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

        
string cn_bh = "bihua:\"";
        string cn_py = "pingying:\"";

        
string s = context.Server.UrlDecode(context.Request.QueryString["s"]);

        
try
        {
            
char[] c = s.ToCharArray();

            
for (int i = 0; i < c.Length; i++)
            {
                
try
                {
                    
int bh = ChineseChar.GetStrokeNumber(c[i]);
                    cn_bh 
+= bh.ToString();
                    
break;
                }
                
catch { }
            }

            
for (int i = 0; i < c.Length; i++)
            {
                
try
                {
                    ChineseChar x 
= new ChineseChar(c[i]);
                    ReadOnlyCollection
<string> roc = x.Pinyins;
                    
foreach (string py in roc)
                    {
                        cn_py 
+= py.Substring(01);
                        
break;
                    }
                }
                
catch { }
            }
        }
        
finally
        {
            context.Response.ContentType 
= "text/plain";
            context.Response.Write(
"{" + cn_py + "\"," + cn_bh + "\"}");
            context.Response.End();
        }
    }
 
    
public bool IsReusable {
        
get {
            
return false;
        }
    }

 

5、测试一下  http://localhost:/../ChnCharInfo.ashx?s=汉字,如果一切正常会显示 {pingying:"HZ",bihua:"5"}

这样就完成了这个小功能,是不是很简单,呵。。

ChnCharInfo.dll、ChnCharInfoResource.dll文件可在附件中下载得到。


附件:/Files/relax/ChnCharInfo.rar

相关文章:

  • 2021-06-13
  • 2021-06-14
  • 2021-09-21
  • 2021-12-11
  • 2022-12-23
  • 2022-12-23
  • 2021-11-22
  • 2022-01-02
猜你喜欢
  • 2022-02-09
  • 2021-11-17
  • 2022-12-23
  • 2021-08-04
  • 2022-01-05
相关资源
相似解决方案