1. First, add the following statement in <system. Web> in Web. config. Otherwise, the message "the request format cannot be recognized" is displayed"
<WebServices>
<Protocols>
<Add name = "httppost"/>
<Add name = "httpget"/>
</Protocols>
</WebServices>
2. Create a "Web Service" Page, such as the testwebservice. asmx file, and add the following method:
C # code
[Webmethod]
Public String checkaccount (string struseraccount)
{
Long lngusercount = 0;
Sqlparameter paramuseraccount = new sqlparameter ("@ useraccount", sqldbtype. varchar, 50 );
Paramuseraccount. value = struseraccount;
Lngusercount = convert. toint64 (sqlhelper. executescalar (dbconnection. sqlserverconnection, commandtype. Text, "select count (*) from user_reg where useraccount = @ useraccount, paramuseraccount ));
If (lngusercount = 0)
Return "OK ";
Else
Return "this user already exists! ";
}
3. create a new "Web form", such as testform. aspx: Add a "text box" server control on this page and name it txaccount. Then add the following statement to
<SCRIPT charset = "UTF-8" type = "text/JavaScript" src = "scripts/jquery/jquery-1.4.2.min.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){
$ ('# Txtaccount'). Blur (function (){
VaR useracc = Document. getelementbyid ('txtaccount'). value;
$. Ajax ({
Type: "Get ",
URL: "WebService/testwebservice. asmx/checkaccount? Struseraccount = "+ useracc,
Data: NULL,
Datatype: "text ",
Success: function (result ){
Alert ("success:" + result );
},
Error: function (result ){
Alert ("error:" + result. responsetext );
}
});
});
});
</SCRIPT>
Or
Jquery code
<SCRIPT charset = "UTF-8" type = "text/JavaScript" src = "scripts/jquery/jquery-1.4.2.min.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){
$ ('# Txtaccount'). Blur (function (){
VaR useracc = Document. getelementbyid ('txtaccount'). value;
$. Ajax ({
Type: "Post ",
URL: "WebService/testwebservice. asmx/checkaccount ",
Data: "struseraccount =" + useracc,
Datatype: "text ",
Success: function (result ){
Alert ("success:" + result );
},
Error: function (result ){
Alert ("error:" + result. responsetext );
}
});
});
});
</SCRIPT>
4. Run the test