一个ajax例子模拟一个注册页面的查看用户是否存在

来源:互联网 发布:老版的龙霸网络电视 编辑:程序博客网 时间:2024/06/10 06:26


/*
第一步:新建一个数据库在数据库中建一个表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();
        }

    }

}
 

原创粉丝点击