一个ajax例子模拟一个注册页面的查看用户是否存在
2007-04-09 14:01
696 查看
/*
第一步:新建一个数据库在数据库中建一个表ybb,用户名:name varchar(20);我是用SQL数据库;
第二步:
新建一个工程把下这拷进去就能运行
在web.config :当然你还要在网上下一个Ajax.dll文件
<system.web>
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
</httpHandlers>
*/
///aspx代码
<%@ 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">
<script language="javascript">
function resqos(res)
{
document.getElementById("display1").innerHTML = res.value;
}
function add()
{
_Default.add(document.getElementById("Text1").value,resqos);//这里有两个参数,第二个是回调 javascript的函数resqos(res),自然res就是_Default.add返加的值
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="width: 170px; height: 7px"><asp:Label ID="Label1" runat="server" Text="用戶呢稱:"></asp:Label></td>
<td style="height: 7px; width: 203px;"><input id="Text1" type="text" /></td>
<td style="width: 428px; height: 7px;">
<a href="javascript:add();void(0);">用户名是否存在??</a>
</td>
<td><span id="display1" style="font-family:courier;">返加的消息</span></td>
</tr>
</table>
</div>
</form>
</body>
</html>
///cs代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;
public partial class _Default : System.Web.UI.Page
{
DbCommand com;
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(_Default));///注意这里注册这个类_Default
com= ybkClass.con();
}
[Ajax.AjaxMethod]
public string add(string text)
{
com = ybkClass.con();
com.CommandText = string.Format("select * from ybb where name='{0}'", text);
string strr;
try
{
if (!(ybkClass.table(com)))
{
strr = "<b><font color=red>恭喜您!用戶名可用!!</font></b>";
}
else
{
strr = "<b><font color=red>用戶名以存在??</font></b>";
}
}
catch (Exception eee)
{
strr = eee.Message;
}
return strr;
}
}
public class ybkClass
{
public static string consql = ConfigurationManager.ConnectionStrings["sql"].ProviderName;///根据自己定我用是.Net2005这是新的东东
public static string conn = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
public ybkClass()
{
}
public static DbCommand con()
{
DbProviderFactory dpc = DbProviderFactories.GetFactory(consql);
DbConnection dbc = dpc.CreateConnection();
dbc.ConnectionString = conn;
DbCommand dc = dbc.CreateCommand();
return dc;
}
public static bool table(DbCommand con)
{
try
{
con.Connection.Open();
con.ExecuteNonQuery();
DbDataReader ddr = con.ExecuteReader();
return ddr.HasRows;
}
catch (Exception ee)
{
throw new Exception(ee.Message);
}
finally
{
con.Connection.Close();
}
}
}
第一步:新建一个数据库在数据库中建一个表ybb,用户名:name varchar(20);我是用SQL数据库;
第二步:
新建一个工程把下这拷进去就能运行
在web.config :当然你还要在网上下一个Ajax.dll文件
<system.web>
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
</httpHandlers>
*/
///aspx代码
<%@ 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">
<script language="javascript">
function resqos(res)
{
document.getElementById("display1").innerHTML = res.value;
}
function add()
{
_Default.add(document.getElementById("Text1").value,resqos);//这里有两个参数,第二个是回调 javascript的函数resqos(res),自然res就是_Default.add返加的值
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="width: 170px; height: 7px"><asp:Label ID="Label1" runat="server" Text="用戶呢稱:"></asp:Label></td>
<td style="height: 7px; width: 203px;"><input id="Text1" type="text" /></td>
<td style="width: 428px; height: 7px;">
<a href="javascript:add();void(0);">用户名是否存在??</a>
</td>
<td><span id="display1" style="font-family:courier;">返加的消息</span></td>
</tr>
</table>
</div>
</form>
</body>
</html>
///cs代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;
public partial class _Default : System.Web.UI.Page
{
DbCommand com;
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(_Default));///注意这里注册这个类_Default
com= ybkClass.con();
}
[Ajax.AjaxMethod]
public string add(string text)
{
com = ybkClass.con();
com.CommandText = string.Format("select * from ybb where name='{0}'", text);
string strr;
try
{
if (!(ybkClass.table(com)))
{
strr = "<b><font color=red>恭喜您!用戶名可用!!</font></b>";
}
else
{
strr = "<b><font color=red>用戶名以存在??</font></b>";
}
}
catch (Exception eee)
{
strr = eee.Message;
}
return strr;
}
}
public class ybkClass
{
public static string consql = ConfigurationManager.ConnectionStrings["sql"].ProviderName;///根据自己定我用是.Net2005这是新的东东
public static string conn = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
public ybkClass()
{
}
public static DbCommand con()
{
DbProviderFactory dpc = DbProviderFactories.GetFactory(consql);
DbConnection dbc = dpc.CreateConnection();
dbc.ConnectionString = conn;
DbCommand dc = dbc.CreateCommand();
return dc;
}
public static bool table(DbCommand con)
{
try
{
con.Connection.Open();
con.ExecuteNonQuery();
DbDataReader ddr = con.ExecuteReader();
return ddr.HasRows;
}
catch (Exception ee)
{
throw new Exception(ee.Message);
}
finally
{
con.Connection.Close();
}
}
}
相关文章推荐
- 一个简单的用ajax验证注册用户名是否存在的例子
- Ajax检测注册用户是否存在
- 毕业设计(十)---用DWR实现 代替AJAX 用户注册检测用户名是否已经存在
- ajax实现检测当前注册用户是否存在
- Ajax检测注册用户是否存在
- 在注册用户时检验用户是否存在的Ajax简单实例
- Ajax检测注册用户是否存在
- jquery与php交互的ajax应用第一课:检测用户注册时用户名是否存在
- Ajax检测注册用户是否存在
- Ajax的应用:用过Ajax来实现不刷新页面的情况下判断用户是否存在
- jquery与php交互的ajax应用第一课:检测用户注册时用户名是否存在
- mybatis学习-入门(6)-实际例子,输入用户注册,并在离开用户名输入框时检查用户是否存在
- 毕业设计(十)---用DWR实现 代替AJAX 用户注册检测用户名是否已经存在
- struts1+js 实现注册一个用户时 此用户名是否存在
- Ajax检测注册用户是否存在
- 用户注册时,Yii利用AJAX页面实时提示用户名已经存在
- jquery与php交互的ajax应用第一课:检测用户注册时用户名是否存在
- jsp注册页面,Ajax实现验证用户名是否存在,密码是否一致,当不存在时,注册按钮变成灰色。
- 利用ajax技术验证数据是否存在的一个简单例子
- PHP+Ajax检测用户名或邮件注册时是否已经存在实例教程