. Net, jquery, ajax, and wcf enable local refresh of Database User Name detection, jquerywcf
Jquery code
$ (Function () {$ ("# user_name"). blur (function () {var user_name = $ ("# user_name"). val (); if (user_name! = "") {$. Ajax ({type: "POST", url: "Handler. ashx ", data: {name: user_name}, beforeSend: function () {}, success: function (msg) {if (msg =" nohave ") {$ ("# usernamee "). attr ("class", "form-group has-error"); $ ("# usernamecheck" ).html ("this user name does not exist! "); $ (" # Showd "). val (" 1 ");} else if (msg =" have "){
$ ("# Usernamee"). attr ("class", "controls ");
$ ("# Usernamecheck" pai.html ("");
$ ("# Showd"). val ("0 ");
}}});}});});
This jquery event is triggered when the textbox with id user_name loses focus. The user_name value is post to Handler. ashx and different situations are determined based on the value returned by Handler. ashx.
Handler. ashx code
public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request["name"].ToString()!="") { string username = context.Request["name"].ToString(); WS.IserviceClient client = new WS.IserviceClient(); string sqlstr = "select * from SystemUser where username='" + username + "'"; int k = client.SqlCmd(sqlstr); if(k>0) { context.Response.Write("have"); } else { context.Response.Write("nohave"); } } } public bool IsReusable { get { return false; }
This code is used to access the wcf Service hosted in iis. It checks whether the input user name exists and sends the user name to Handler through ajax post. handler. ashx references the WCF Service WS and instantiates a WS. the new IserviceClient object client calls the SqlCmd method in the wcf Service. If the user name does not exist, the bootstrap error style is displayed. If the user name does not exist, it is not processed if it is correct.