zoukankan      html  css  js  c++  java
  • [转].Net ajax检测用户名是否重复

    LoginValidate.aspx
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoginValidate.aspx.cs" Inherits="LoginValidate" %>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>验证用户名是否存在</title>
    <script type="text/javascript">
        var xmlHttp;
        function createXMLHttpRequest()
        {
            if(window.ActiveXObject)
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else if(window.XMLHttpRequest)
            {
                xmlHttp = new XMLHttpRequest();
            }
        }
        //处理方法
        function CheckUserName()
        {
            createXMLHttpRequest();
            var url= "LoginValidate.ashx?username="+document.getElementById("username").value;
            xmlHttp.open("GET",url,true);
            xmlHttp.onreadystatechange=ShowResult;
            xmlHttp.send(null);
            //document.getElementById("Msg").innerHTML='';
        }
        //回调方法
        function ShowResult()
        {
            if(xmlHttp.readyState==4)
            {
                if(xmlHttp.status==200)
                {
                    document.getElementById("Msg").innerHTML=xmlHttp.responseText;
                }
            }
        }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <table style=" 487px">
        <tr>
            <td style=" 70px">
                用户名:</td>
            <td style=" 231px"><input id="username" type="text" />
            <input id="Button1" type="button" value="button" onclick="CheckUserName();" /></td>
            <td id="Msg"></td>
        </tr>
        <tr>
            <td style=" 70px">
            </td>
            <td style=" 231px">
            </td>
            <td>
                </td>
        </tr>
    </table>
    </div>
    </form>
    </body>
    </html>

    服务器端代码如下:(这里我是用的临时处理文件.ashx)
    LoginValidate.ashx
    <%@ WebHandler Language="C#" Class="LoginValidate" %>

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

    public class LoginValidate : IHttpHandler
    {
       
        public void ProcessRequest (HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string username = context.Request.QueryString["username"].ToString();
            string strSQL = "select username from users where username='" + username + "'";
            if (ReDataSet(strSQL).Tables[0].Rows.Count > 0)
            {
                context.Response.Write("该用户已经有人使用!");
            }
            else
            {
                context.Response.Write("恭喜你!"+username+"可以使用!");
            }
            System.Threading.Thread.Sleep(3000);
        }

        //数据库连接字符串
        public static string strCon = "Data Source=.;database=exam;uid=sa;pwd=;";

        /// <summary>
        /// 执行SQL语句,返回DataSet
        /// </summary>
        /// <param name="strSQL"></param>
        /// <returns></returns>
        public  DataSet ReDataSet(string strSQL)
        {
            SqlConnection con = new SqlConnection(strCon);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(strSQL, con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
       
        /// <summary>
        /// 不重复调用
        /// </summary>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
  • 相关阅读:
    【JVM性能调优】检测最耗cpu的线程的脚本
    JUC之ThreadPoolExecutor实现原理
    HashMap实现原理
    JUC之阻塞队列BlockingQueue的实现原理
    dubbo实践
    .net 技术基础
    日志等级
    CentOS 笔记(六) 历史命令 自动补充
    CentOS 笔记(五) 常用工具
    CentOS 笔记(二) 端口占用,进程查看
  • 原文地址:https://www.cnblogs.com/xsmhero/p/1797065.html
Copyright © 2011-2022 走看看