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