Jquery+WebService 校验账号是否已被注册的代码 - Web前端
作者:98wpeu发布时间:2026-06-15分类:网页前端技术浏览:37
导读:详细代码如下:Default.aspx复制代码代码如下:<%@PageLanguage="c#"AutoeventWireup="true"codeBehind=...
详细代码如下:
Default.aspx
复制代码 代码如下:
<%@ Page Language="c#" AutoeventWireup="true" codeBehind="Default.aspx.cs" Inherits="Ajax_xml._Default" %>
<!DOCtypehtml 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="width:100%;">
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:TextBox ID="UserID" runat="server"></asp:TextBox>
<asp:label ID="IsExist" runat="server" Visible="true"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</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;
}
}
}
}
Default.aspx
复制代码 代码如下:
<%@ Page Language="c#" AutoeventWireup="true" codeBehind="Default.aspx.cs" Inherits="Ajax_xml._Default" %>
<!DOCtypehtml 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="width:100%;">
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:TextBox ID="UserID" runat="server"></asp:TextBox>
<asp:label ID="IsExist" runat="server" Visible="true"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</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;
}
}
}
}
相关推荐
- jQuery $命名冲突解决方案汇总 - Web前端
- 推荐25个超炫的jQuery网格插件 - Web前端
- 用jquery模仿的a的title属性的例子 - Web前端
- 一款基jquery超炫的动画导航菜单可响应单击事件 - Web前端
- jQuery前端框架easyui使用Dialog时bug处理 - Web前端
- JQuery设置获取下拉菜单某个选项的值(比较全) - Web前端
- Jquery设置attr的disabled属性控制某行显示或者隐藏 - Web前端
- jQuery判断checkbox是否选中的3种方法 - Web前端
- 事件委托与阻止冒泡阻止其父元素事件触发 - Web前端
- jQuery截取指定长度字符串代码 - Web前端
- 网页前端技术排行
-
- 1【第六章】Foundation之按钮和下拉功能 - Web前端
- 2jQuery编写widget的一些技巧分享 - Web前端
- 3在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 4基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 5分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 6jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 7[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 8JS网页制作实例:标签云 - Web前端
- 9分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


