Are simple examples, so send the code directly
Static page ajax.html
Copy Code code as follows:
<title>Ajax</title>
<script type= "Text/javascript" >
function Loadxmldoc () {
if (document.getElementById ("account"). Value = = "") {
document.getElementById ("Accdiv"). InnerHTML = "User name cannot be empty";
Return
}
var xmlHttp;
if (window. XMLHttpRequest) {//code for ie7+
XmlHttp = new XMLHttpRequest ();
}
else {//code for IE5/IE6
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
Xmlhttp.onreadystatechange = function () {
if (xmlhttp.readystate = = 4 && xmlhttp.status = 200) {
document.getElementById ("Mydiv"). Innerhtml=xmlhttp.responsetext;
if (Xmlhttp.responsetext = = "true") {
document.getElementById ("Accdiv"). InnerHTML = "User name not available";
}
else {
document.getElementById ("Accdiv"). InnerHTML = "user name available";
}
}
}
var a = document.getElementById ("account"). Value;
Get
Xmlhttp.open ("Get", "validate.aspx?account=" + A + "&random=" + Math.random, True);
Xmlhttp.send ();
}
function Deldata () {
document.getElementById ("Account"). Value = "";
document.getElementById ("Accdiv"). InnerHTML = "";
}
</script>
<body>
<table>
<tr>
<td> accounts: </td><td><input id= "Account" type= "text" onblur= "Loadxmldoc ();" onfocus= "Deldata ();" /></td><td><div id= "Accdiv" ></div></td>
</tr>
<tr>
<td> Password: </td><td><input id= "passwd" type= "password"/></td>
</tr>
<tr>
<td> Confirm Password: </td><td><input id= "vpasswd" type= "password"/></td>
</tr>
<tr>
<td> Name: </td><td><input id= "name" type= "text"/></td>
</tr>
</table>
</body>
Call function when the account input box loses focus
The access server uses a Get method, so additional random code is used at the parameter to avoid caching.
Verify page validate.aspx background code:
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Configuration;
Using SYSTEM.DATA.SQL;
Using System.Data.SqlClient;
public partial class Ajax_validate_validate:System.Web.UI.Page
{
Public SqlConnection Conn;
protected void Page_Load (object sender, EventArgs e)
{
Response.Clear ();
if (Exists (request.querystring["account"))
Response.Write ("true");
Else
Response.Write ("false");
Response.End ();
}
<summary>
Getting a database connection
</summary>
<returns></returns>
Protected SqlConnection getconnection ()
{
String str = configurationmanager.connectionstrings["ConnectionString"]. ConnectionString;
conn = new SqlConnection (str);
Return conn;
}
protected bool Exists (string account)
{
using (getconnection ())
{
Try
{
Conn. Open ();
String sqlstr = "SELECT count (*) from userinfo where account= '" + account + "";
SqlCommand cmd = new SqlCommand (SQLSTR, conn);
int row = Convert.ToInt32 (cmd. ExecuteScalar ());
if (Row > 0)
return true;
Else
return false;
}
catch (Exception e)
{
Throw e;
}
Finally
{
Conn. Close ();
}
}
}
}
Verify in the background that the user name already exists in the database, returns TRUE or False
Run Result:
The database is simple, with only one table UserInfo, 3 fields: Account, passwd, name
Note: When you write data in the background to the request page, when you finish writing the data you want to send, you need to call the Response.End () method to terminate the write, or you may send a full page past.