Ajax: obtain the first character and stroke of Chinese characters and Pinyin

Source: Internet
Author: User

A small feature will be added to a recent project, that is, when the customer enters the name of the drug, it will automatically generate the first character of the medicinal material name and the number of strokes in the first word of the medicinal material name, it will be used in future medicinal material search.

To facilitate the entry of medicinal material information, I want to use ajax to solve the problem.Visual Studio International packClass Library.

1, ReferenceChncharinfo. dll,Chncharinforesource. dll to the web Project:


2. Reference The jquery script library on the page and add a JS function:

Chncharinfo

<ScriptSRC= "UI/JS/jquery. js"Type="Text/JavaScript"Language="Javascript"></SCRIPT>

<Script language = "JavaScript" type = "text/JavaScript">

<! --
Function Chncharinfo (o ){
$. Getjson (
" Chncharinfo. ashx? S = "   + Encodeuri (O. value ),
Function (JSON ){
Try {
$ ( "# Medspell " ). Value = JSON. pingying; // pinyin
$ ( "# Medpen " ). Value = JSON. bihua; // stroke
} Catch (E ){}
}
);
}
// -->
< /

 

3. Add the onblur event to the textbox: textbox1.attributes. Add ("onblur", "chncharinfo (this )");

4. Add a chncharinfo. ashx file:

 

Chncharinfo
<% @ Webhandler Language = " C # " Class = " Chncharinfo "   %>

UsingSystem;
UsingSystem. Web;

UsingSystem. Collections. objectmodel;
UsingMicrosoft. International. converters. pinyinconverter;

Public ClassChncharinfo: ihttphandler {

Public VoidProcessrequest (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 ( 0 , 1 );
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. Test http: // localhost: //./chncharinfo. ashx? S = Chinese character. If everything is normal, it will be displayed{Pingying: "HZ", bihua: "5 "}

This completes this small function, isn't it very simple ..

Chncharinfo. dll,You can download the chncharinforesource. dll file from the attachment.


Attachment:/files/relax/chncharinfo.rar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.