asp.net ajax 2.0实现用户名的检索

需要的文件:Complex.ashx,2_UseAsyncCommLayer.aspx

Complex.ashx中代码如下:

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

using System;
using System.Web;
using System.Data.SqlClient;
public class Complex : IHttpHandler
{
   
    public void ProcessRequest (HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string action = context.Request.Headers["action"];
        if (action == "normal")
        {
             try
            {
                string StrConn = "server=XIATIAN;database=GUOFENG;uid=sa;pwd=sa";
                SqlConnection conn = new SqlConnection(StrConn);
                conn.Open();
               
                string StrSql = "select count(*) from Users where UsersName='" + context.Request.Params["data"].ToString() + "'";
                SqlCommand comm = new SqlCommand(StrSql, conn);
                int i = (int)comm.ExecuteScalar();
                if (i > 0)
                {
                    context.Response.Write("存在");
                }
                else
                {
                    context.Response.Write("不存在");
                }
            }
            catch(Exception ex)
            {
                context.Response.Write(ex);
            }
        }
         else
        {
            System.Threading.Thread.Sleep(10000);
        }
       
       
    }
 
    public bool IsReusable
    {
        get {
            return false;
        }
    }

}

 

2_UseAsyncCommLayer.aspx中代码如下

<script language="javascript" type="text/javascript">
        var webRequest=null;
        function sendRequest(action)
        {
            webRequest=new Sys.Net.WebRequest();
            webRequest.set_url("handlers/Complex.ashx");
            webRequest.get_headers()["action"]=action;
            webRequest.set_body("data="+encodeURIComponent(document.getElementById("txt1").value));
            webRequest.set_httpVerb("POST");
            webRequest.set_timeout(3000);
           
            webRequest.add_completed(onCompleted);
            webRequest.invoke();
        }
        function onCompleted(response,eventArgs)
        {
            if(response.get_aborted())
            {
                alert("Response Aborted!");
            }
            else if(response.get_responseAvailable())
            {
                var statusCode=response.get_statusCode();
                if((statusCode<200)||(statusCode>=300))
                {
                    alert("Error occurred");
                }
                else
                {
                    alert(response.get_responseData());
                }
            }
            else if(response.get_timedOut())
            {
                alert("Request timed out!");
            }
            else
            {
                alert("Error occurred!");
            }
        }
    </script>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="scriptmanager1" runat="server" ></asp:ScriptManager>
    <input  type="text" id="txt1"   οnblur="sendRequest('normal')"/>
    
       </div>
    </form>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值