日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Ajax检测注册用户是否存在

發(fā)布時間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ajax检测注册用户是否存在 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
HTML代碼如下:
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="width: 487px">
? ? <tr>
? ?? ???<td style="width: 70px">
? ?? ?? ?? ?用戶名:</td>
? ?? ???<td style="width: 231px"><input id="username" type="text" />
? ?? ???<input id="Button1" type="button" value="button" οnclick="CheckUserName();" /></td>
? ?? ???<td id="Msg"></td>
? ? </tr>
? ? <tr>
? ?? ???<td style="width: 70px">
? ?? ???</td>
? ?? ???<td style="width: 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("該用戶已經(jīng)有人使用!");
? ?? ???}
? ?? ???else
? ?? ???{
? ?? ?? ?? ?context.Response.Write("恭喜你!"+username+"可以使用!");
? ?? ???}
? ?? ???System.Threading.Thread.Sleep(3000);
? ? }

? ? //數(shù)據(jù)庫連接字符串
? ? public static string strCon = "Data Source=.;database=exam;uid=sa;pwd=;";

? ? /// <summary>
? ? /// 執(zhí)行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;
? ?? ???}
? ? }
}

轉載于:https://www.cnblogs.com/beniao/archive/2008/03/20/1114020.html

總結

以上是生活随笔為你收集整理的Ajax检测注册用户是否存在的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。