ASP.NET中ajax驗證用戶名和郵箱是否重複

這個是前臺顯示的頁面代碼↓javascript

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>
    <script language="javascript" type="text/javascript">

        function neihe() {//設置瀏覽器的內核
            if (window.ActiveXObject) {//若是是IE瀏覽器內核
                return new ActiveXObject("Microsoft.XMLHTTP");
            } else if (window.XMLHttpRequest) {//不然
                return new XMLHttpRequest();
            }
        }
        var XMLduixiang;
        function fasong(name) {
            if (name != "") {
                var url = encodeURI("houtai.aspx?chuandi=" + name); //encodeURI對URL編碼
                XMLduixiang = neihe(); //建立XMLGTttpRequest
                XMLduixiang.onreadystatechange = huidiao; //回傳處理結果(請求被髮送到服務器時)
                XMLduixiang.open("GET", url, true); //初始化XMLGTttpRequest組件
                //最後的true(異步)或 false(同步)
                XMLduixiang.send(null);//發送請求
            }
        }
        function huidiao(){
            if(XMLduixiang.readyState==4 && XMLduixiang.status == 200){
                var cljg = XMLduixiang.responseText;
                //responseText得到字符串形式的響應數據
                //responseXML得到 XML 形式的響應數據
                if(cljg=="true"){//若是返回真
                    document.getElementById("ts").style.display = "none";
                } else {//不然
                    document.getElementById("ts").style.display = "inline";
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        輸入用戶名:
        <input type="text"  onblur="fasong(this.value)" /><%--失去焦點--%>
        <span id="ts" style=" display:none; color:Red;">沒有這個用戶</span>
        
    </div>
    </form>
</body>
</html>

 

 

這個是後臺頁面的代碼↓html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class houtai : System.Web.UI.Page
{
    string sjklj = ConfigurationManager.ConnectionStrings["ceshi"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/plain";//設置輸出類型
        if (Request.QueryString["chuandi"] != null)
        {
            string name = Request.QueryString["chuandi"].ToString().Trim();
            if (name.Length>0)
            {
                if (shifou(name))
                {
                    Response.Write("true");
                }
                else
                {
                    Response.Write("false");
                }
            }
            else
            {
                Response.Write("false");
            }
        }
        else
        {
            Response.Write("false");
        }
    }

    protected bool shifou(string name)
    {
        string sql = "select * from cs where name=@name";
        using (SqlConnection conn = new SqlConnection(sjklj))
        {
            conn.Open();

            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddRange(new SqlParameter[] { new SqlParameter("@name", name) });
            if (cmd.ExecuteScalar() == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
相關文章
相關標籤/搜索