zoukankan      html  css  js  c++  java
  • Jquery+WebService 校验账号是否已被注册

    在Javascirpt代码中,调用Jquery的方法$.Ajax(function)实现Ajax,传递账号信息给Web服务,
    Web服务再调用数据库操作类查询数据库,并返回数据给前台页面。详细代码如下:
    Default.aspx
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Ajax_XML._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 type="text/javascript" src="jquery-1.3.2-vsdoc2.js" language="javascript"></script>
    <script type="text/javascript" language="javascript">
        $(
    function() {
            $(
    "#<%=UserID.ClientID%>").keyup(
                    
    function() {
                        $.ajax({
                            type: 
    "POST",
                            contentType: 
    "application/json",
                            dataType: 
    "json",
                            url: 
    "WebService1.asmx/UserIsExist",
                            data: 
    "{UserID:'" + $("#<%=UserID.ClientID%>").val() + "'}",
                            success: 
    function(result) {
                                
    if (result.d == "true")
                                    $(
    "#<%=IsExist.ClientID%>").text("Yes");  //账号已存在
                                
    else
                                    $(
    "#<%=IsExist.ClientID%>").text("No");  
                            }
                          
                        });
                    }
                    );

        })

    </script>
    </head>

    <body>
        
    <form id="form1" runat="server">
        
    <div>
        
            
    <table style="100%;">
                
    <tr>
                    
    <td>
                        
    &nbsp;</td>
                    <td>
                        
    &nbsp;</td>
                    <td>
                        
    &nbsp;</td>
                </tr>
                <tr>
                    
    <td>
                        
    &nbsp;</td>
                    <td>
                        
    <asp:TextBox ID="UserID" runat="server"></asp:TextBox>
                        <asp:Label ID="IsExist" runat="server" Visible="true"></asp:Label>
                    </td>
                    <td>
                        
    &nbsp;</td>
                </tr>
                <tr>
                    
    <td>
                        
    &nbsp;</td>
                    <td>
                        
    &nbsp;</td>
                    <td>
                        
    &nbsp;</td>
                </tr>
            </table>
        
        
    </div>
        </form>
    </body>
    </html>

    WebService1.asmx.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Text;
    using DAL;

    namespace Ajax_XML
    {
        
    /// <summary>
        
    /// WebService1 的摘要说明
        
    /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo 
    = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(
    false)]
        
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
         [System.Web.Script.Services.ScriptService]
        
    public class WebService1 : System.Web.Services.WebService
        {

            [WebMethod]
            
    public string HelloWorld()
            {

                
    return "Hello,World!";
            }
            [WebMethod]
            
    public string UserIsExist(string UserID)
            {
                
                
    string sql = string.Format("select * from Customers where FirstName='" + UserID+"'");

                
    using (SqlDataReader dr = SqlHelper.ExecuteSql(sql))
                {
                    
    if (dr.Read())
                        
    return "false";
                    
    else
                        
    return "true";
                }

                

            }
        }
    }
    SqlHelper.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;

    namespace DAL
    {
        
    /// <summary>
        
    /// 数据库操作类
        
    /// </summary>
        public class SqlHelper
        {  
            
    private static SqlConnection conn;
            
    private static  SqlCommand comm;
            
    private static  SqlDataReader dr;
            
            
    /// <summary>
            
    /// 打开数据库连接
            
    /// </summary>
            public static void ConnOpen()
            {

           
                    
    try
                    {
                        conn 
    = new SqlConnection(ConfigurationManager.ConnectionStrings["BBS"].ConnectionString);
                        conn.Open();
                    }
                    
    catch (Exception e)
                    {
                        Console.WriteLine( e.Message);
                    }
                
            }

            
    /// <summary>
            
    /// 关闭数据库连接,释放资源
            
    /// </summary>
            public static void ConnClose()
            {
                
    if (conn != null)
                {
                    conn.Close();
                }
                
    if (comm != null)
                {
                    comm.Dispose();
                }
            }

            
    public static SqlDataReader ExecuteSql(string sql)
            {
                SqlHelper.ConnOpen();
                comm 
    = new SqlCommand(sql, conn);
                
    try
                {
                    dr 
    = comm.ExecuteReader();
                    
    return dr;
                }
                
    catch (Exception e)
                {
                    
    throw e;
                }
                
            }
        }
    }
  • 相关阅读:
    array_count_values源码
    php 编译安装记录
    mysql 安装的过程做个记录
    初识highcharts 库
    php 不重新编译增加新扩展的方法
    备考PMP
    Beyond Compare4破解--写reg脚本删除注册表
    SourceTree 跳过登录
    正则 (?=exp)
    springmvc--处理器的返回参数
  • 原文地址:https://www.cnblogs.com/bestzrz/p/1775732.html
Copyright © 2011-2022 走看看