文本框异步自动检测数据库

先新建一个页面Defult.aspx页面代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TxT自定义检测数据库_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="scriptmanger1" runat="server">
    <Scripts>
    <asp:ScriptReference Path="~/TxT自定义检测数据库/jscript.js" />
    </Scripts>
    </asp:ScriptManager>
    <div>
        
         <input id="TextName" type="text" onblur="checkname()" />
         <div style="background-color:Blue; font-weight:bold ; color:Red ; display:none" id="loadingflag">正在加载数据........</div><br />
         <span id="NameError" style="color:#ff0033;"></span>
    </div>
    </form>
</body>

</html>
新建一个一般处理文件de:

<%@ WebHandler Language="C#" Class="De" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

public class De : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
       // context.Response.Write("Hello World");

        if (context.Request.QueryString["type"].ToString().Trim() == "1")
        {
            var name = context.Request.QueryString["name"].ToString().Trim();
            var back = Class1.get(name);
            switch (back)
            {
                case 1:
                    context.Response.Write(1);
                    break;
                case 2:
                    context.Response.Write(2);
                    break;
                case 3:
                    context.Response.Write(3);
                    break;
            }
        }
        else
        {
            context.Response.Write(3);
        }  
   
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

js里的代码:

/// <reference name="MicrosoftAjax.js"/>

var xmlhttp=false;
try
{
   xmlhttp=new ActiveXObject("Msxml2.MLHTTP");
}
catch(E)
{
  try
     {
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     catch(E)
     {
     xmlhttp=new XMLHttpRequest();//兼容非IE浏览器,直接创建XMLHTTP对象
     }
}
function checkname()
{
      document.getElementById("NameError").innerHTML="";
      var username=document.getElementById("TextName").value;
      var reg="[^a-zA-Z_0-9]";
      var regl="\\D";
      if(username=="")  //判断不能为空
      {
          document.getElementById("NameError").innerHTML="<font color=\"red\">用户名不能空!</font></br>";
          return;
      }
      else{// 向服务器发送。。
       chened();
       }     

}
function chened()
{
  var name=document.getElementById("TextName").value;
  var strurl="De.ashx?type=1&name="+name;
  xmlhttp.open("GET",strurl,true);
  xmlhttp.onreadystatechange=function ()
  {
    if(xmlhttp.readystate==4 && xmlhttp.status==200)
    {
        var date=xmlhttp.responseText;          
          if(date!="")
            {
             switch(date)
             {
               case "1":
                    document.getElementById("NameError").innerHTML="<font color=\"red\">此用户存在</font></br>";
                    document.getElementById("loadingflag").style.display='none';
                   break;
               case "2":
                   document.getElementById("NameError").innerHTML="<font color=\"red\">此用户不存在!</font></br>";
                    document.getElementById("loadingflag").style.display='none';
                   break;
               case "3":
                   document.getElementById("NameError").innerHTML="<font color=\"red\">系统错误!</font></br>";
                    document.getElementById("loadingflag").style.display='none';
                   break;       
               }   
              }
          }
    }
      xmlhttp.send(null);
      document.getElementById("loadingflag").style.display='none';  

}

连接数库据基础类:
 

 public static  string con = "data source=.;database=OfficeData;uid=sa;pwd=1234";
   // public  string  sql = "";
    public static  SqlConnection mycon = new SqlConnection();
    public static  SqlCommand mycom = new SqlCommand();
    public static int get(string name)
    {
        try
        {

            if (name.Length < 0 || name != "")
            {
                string sql = String.Format("SELECT * FROM UserTable WHERE (UserName = '{0}')", name);
               // string sql = "";
                mycon.ConnectionString =con;
                mycon.Open();
                mycom.Connection = mycon;
                mycom.CommandText = sql;
                int i=Convert.ToInt32(mycom.ExecuteScalar());
                if (i>0)
                    return 1;//表示有数据
                else
                {
                    return 2;//表示没有数据
                }

            }
            else
                return 3;
        }
        catch (Exception)
        {
            return 3;
        }
        finally
        {
            mycon.Close();
        }
   
    }


 

posted @ 2008-04-18 16:55 冷明军 阅读(954) 评论(4) 编辑 收藏