熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

Ajax檢測注冊用戶是否存在

2022-06-13   來源: .NET編程 
    原帖及討論
    HTML代碼如下
    LoginValidateaspx
    <%@ Page Language=C# AutoEventWireup=true CodeFile=LoginValidateaspxcs Inherits=LoginValidate %>
    <html xmlns= >
    <head runat=server>
    <title>驗證用戶名是否存在</title>
    <script type=text/javascript>
    var xmlHttp;
    function createXMLHttpRequest()
    {
    if(windowActiveXObject)
    {
    xmlHttp = new ActiveXObject(MicrosoftXMLHTTP
    }
    else if(windowXMLHttpRequest)
    {
    xmlHttp = new XMLHttpRequest()
    }
    }
    //處理方法
    function CheckUserName()
    {
    createXMLHttpRequest()
    var url= LoginValidateashx?username=+documentgetElementById(usernamevalue;
    xmlHttpopen(GETurltrue)
    xmlHttponreadystatechange=ShowResult;
    xmlHttpsend(null)
    //documentgetElementById(MsginnerHTML=;
    }
    //回調方法
    function ShowResult()
    {
    if(xmlHttpreadyState==
    {
    if(xmlHttpstatus==
    {
    documentgetElementById(MsginnerHTML=xmlHttpresponseText;
    }
    }
    }
    </script>
    </head>
    <body>
    <form id=form runat=server>
    <div>
    <table >
    <tr>
    <td >
    用戶名</td>
    <td ><input id=username type=text />
    <input id=Button type=button value=button onclick=CheckUserName() /></td>
    <td id=Msg></td>
    </tr>
    <tr>
    <td >
    </td>
    <td >
    </td>
    <td>
    </td>
    </tr>
    </table>
    </div>
    </form>
    </body>
    </html>
    服務器端代碼如下(這裡我是用的臨時處理文件ashx)
    LoginValidateashx
    <%@ WebHandler Language=C# Class=LoginValidate %>
    using System;
    using SystemWeb;
    using SystemData;
    using SystemDataSqlClient;
    public class LoginValidate : IHttpHandler
    {
    public void ProcessRequest (HttpContext context)
    {
    contextResponseContentType = text/plain;
    string username = contextRequestQueryString[username]ToString()
    string strSQL = select username from users where username= + username + ;
    if (ReDataSet(strSQL)Tables[]RowsCount >
    {
    contextResponseWrite(該用戶已經有人使用!
    }
    else
    {
    contextResponseWrite(恭喜你!+username+可以使用!
    }
    SystemThreadingThreadSleep(
    }
    //數據庫連接字符串
    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
    {
    conOpen()
    SqlDataAdapter da = new SqlDataAdapter(strSQL con)
    DataSet ds = new DataSet()
    daFill(ds)
    return ds;
    }
    catch (Exception ex)
    {
    throw new Exception(exMessage)
    }
    finally
    {
    conClose()
    }
    }
    /// <summary>
    /// 不重復調用
    /// </summary>
    public bool IsReusable
    {
    get
    {
    return false;
    }
    }
    }
From:http://tw.wingwit.com/Article/program/net/201311/11585.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.