asp.net Anthem 无刷新检测注册用户名

 

 

本实例用到的是Anthem Ajax框架来实现无刷新检测注册用户名的。Anthem Ajax下载地址:

http://sourceforge.net/projects/anthem-dot-net

添加User类:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.SqlClient;

 public bool CheckUser (string strUserName)
        {

            SqlConnection con = new SqlConnection("Server=zhan;database=blog;uid=sa;pwd=zhan");
            SqlCommand cmd = new SqlCommand("select * from Blog_User where UserName='" + strUserName + "'",con);
            cmd.Connection.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                return true;
            }
            else
            {
                return false;
            }

        }

reg.aspx 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="reg.aspx.cs" Inherits="Front_reg" %>
<%@ Register Assembly="Anthem" Namespace="Anthem" TagPrefix="anthem" %>
<!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>
  <title>博客</title>

    <STYLE type="text/css">#layout_0 { FLOAT: left; WIDTH: 22% }
 #layout_1 { FLOAT: left; WIDTH: 72% }
 #layout_3 { FLOAT: left; WIDTH: 0% }
 .albumalert { BORDER-RIGHT: #777 1px solid; BORDER-TOP: #777 1px solid; BACKGROUND: #fff; BORDER-LEFT: #777 1px solid; WIDTH: 250px; BORDER-BOTTOM: #777 1px solid; HEIGHT: 150px }
 .alerttitle { PADDING-RIGHT: 6px; PADDING-LEFT: 6px; font-SIZE: 14px; BACKGROUND-IMAGE: url(/images/quickwindow.jpg); PADDING-BOTTOM: 6px; COLOR: #000; PADDING-TOP: 6px; BACKGROUND-REPEAT: repeat-x; HEIGHT: 22px; TEXT-ALIGN: left }
 .alertcentent { LINE-HEIGHT: 30px }
 CODE { BORDER-RIGHT: #282828 1px solid; PADDING-RIGHT: 1em; BORDER-TOP: #282828 1px solid; DISPLAY: block; PADDING-LEFT: 1em; BACKGROUND: #222; PADDING-BOTTOM: 1em; MARGIN: 0.5em; font: 1em Fixedsys; BORDER-LEFT: #282828 1px solid; COLOR: #888; PADDING-TOP: 1em; BORDER-BOTTOM: #282828 1px solid }
 CODE SPAN { COLOR: #666; font-FAMILY: Verdana }
 #fixed { RIGHT: 0px; POSITION: fixed; TOP: 120px }
 </STYLE>
    <META content="MShtml 6.00.2900.3059" name="GENERATOR">
    <link href="../Blog.css" rel="stylesheet" type="text/css" />
    </head>
 <body οndragstart="return false" οnlοad="initExobud();">
 <form id="Form1" runat ="server" >
  <div id="fixed" style="DISPLAY: none" align="center">
  
  </div>
  <font face="宋体"></font><font face="宋体"></font>
    <div id="topPadding"></div>
    <div id="header" style="height: 101px">
                    <img src="../css/title11-02-58.jpg" /></div>
    <div id="navBar">
     <div id="innerNavBar" style="height: 22px; width: 980px;">
     </div>
    </div>
    <div id="mainWrapper">
                    <div style="width: 379px; height: 306px">
                    <table style="width: 854px; position: static; height: 201px">
                        <tr>
                            <td colspan="4" style="height: 25px; text-align: center">
                </td>
                        </tr>
                        <tr>
                            <td colspan="4" style="height: 34px; text-align: center; font-size: 16px;">
                                <asp:Label ID="Label1" runat="server" Font-Size="Medium" Text="用户注册" Width="111px"></asp:Label></td>
                        </tr>
                        <tr>
                            <td colspan="4" style="font-size: 16px; height: 7px; text-align: center">
                            </td>
                        </tr>
        <tr>
            <td style="width: 389px; text-align: right; height: 22px;">
                用户名:</td>
            <td style="width: 132px; height: 22px;">
                <anthem:TextBox ID="txtUserName" runat="server" Style="position: static" Width="150px" AutoCallBack ="true"  OnTextChanged="TextBox1_TextChanged" ></anthem:TextBox></td>
            <td style="width: 179px; height: 22px; text-align: left;">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtUserName"
                    Display="Dynamic" ErrorMessage="用户名不能为空!" Width="98px"></asp:RequiredFieldValidator>
                    <anthem:Label ID="labResult" runat="server" Width="201px" Font-Size="Small" Height="16px"></anthem:Label></td>
            <td style="width: 116px; height: 22px">
            </td>
        </tr>
        <tr>
            <td style="width: 389px; height: 12px; text-align: right">
                密码:</td>
            <td style="width: 132px; height: 12px">
                <anthem:TextBox ID="txtPwd" runat="server" AutoCallBack ="true" TextMode="Password" Width="150px" OnTextChanged="txtPwd_TextChanged"></anthem:TextBox></td>
            <td style="width: 179px; height: 12px">
                <anthem:Label ID="lblPwdLength" runat="server" Font-Size="Small" Width="190px"></anthem:Label>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtPwd"
                    Display="Dynamic" ErrorMessage="密码不能为空!"></asp:RequiredFieldValidator></td>
            <td style="width: 116px; height: 12px; text-align: center;">
                &nbsp;<select id="lstPic" name="lstPic" οnchange="document.images['face'].src=options[selectedIndex].value;" runat="server" style="width: 41px; position: static">
                                </select></td>
        </tr>
        <tr>
            <td style="width: 389px; text-align: right; height: 2px;">
                性别:</td>
            <td style="width: 132px; height: 2px;">
                <anthem:RadioButton ID="RadioButton1" runat="server" Style="position: static" Text="帅哥" GroupName="sex" />
                <anthem:RadioButton ID="RadioButton2" runat="server" Style="position: static" Text="美女"
                    Width="60px" GroupName="sex" /></td>
            <td style="width: 179px; height: 2px;">
            </td>
            <td rowspan="7" style="width: 116px">
                <img alt="个人头像" id="face" src="" runat="server" style="position: static" /></td>
        </tr>
        <tr>
            <td style="width: 389px; text-align: right; height: 9px;">
                邮箱:</td>
            <td style="width: 132px; height: 9px;">
                <anthem:TextBox ID="txtEmail" runat="server" Style="position: static" Width="150px"></anthem:TextBox></td>
            <td style="width: 179px; height: 9px;">
                <anthem:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                    ControlToValidate="txtEmail" ErrorMessage="邮箱格式不正确!" Font-Size="Small" Style="position: static"
                    ValidationExpression="/w+([-+.']/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*" Width="108px" Display="Dynamic"></anthem:RegularExpressionValidator>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtEmail"
                    Display="Dynamic" ErrorMessage="不能为空!" Width="65px"></asp:RequiredFieldValidator></td>
        </tr>
        <tr>
            <td style="width: 389px; height: 16px; text-align: right">
                验证码:</td>
            <td style="width: 132px; height: 16px">
                <anthem:TextBox ID="txtCode" runat="server" Style="position: static" Width="150px"></anthem:TextBox></td>
            <td style="width: 179px; height: 16px">
            <%--<img  src ="~/Admin/ValidateCode.aspx" οnclick="this.src='~/Admin/ValidateCode.aspx'"/>--%>
                <asp:Image ID="Image1" runat="server" Width="58px"   ImageUrl ="~/Admin/ValidateCode.aspx" Height="20px"/></td>
        </tr>
        <tr>
            <td style="width: 389px; height: 20px;">
            </td>
            <td style="width: 132px; text-align: center; height: 20px;">
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="position: static"
                    Text="确定" Width="49px" CssClass="ButtonCss" />
                &nbsp;<anthem:Button ID="btnCancel"
                        runat="server" Style="position: static" Text="取消" Width="47px" OnClick="btnCancel_Click" CssClass="ButtonCss" />
                </td>
            <td style="width: 179px; height: 20px;">
            </td>
        </tr>
                        <tr>
                            <td style="width: 389px; height: 21px">
                            </td>
                            <td style="width: 132px; height: 21px; text-align: center">
                            </td>
                            <td style="width: 179px; height: 21px">
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 389px; height: 21px">
                            </td>
                            <td style="width: 132px; height: 21px; text-align: center">
                            </td>
                            <td style="width: 179px; height: 21px">
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 389px; height: 21px">
                            </td>
                            <td style="width: 132px; height: 21px; text-align: center">
                            </td>
                            <td style="width: 179px; height: 21px">
                            </td>
                        </tr>
    </table>
                    </div>
  </div>
  </form>
  
 </body>
</html>

 reg.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.SqlClient;
using System.Text.RegularExpressions;
using System.IO;
using System.Drawing;
using System.Globalization;
using System.Resources;

public partial class Front_reg : System.Web.UI.Page
{
    User user = new User();
    protected void Page_Load(object sender, EventArgs e)
    {   

    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

        if (user.CheckUser(this.txtUserName.Text.Trim()))
        {
            labResult.Text = "对不起,该用户名已被使用!";
            txtUserName.ForeColor = Color.Red;
            labResult.ForeColor = Color.Red;
            txtUserName.UpdateAfterCallBack = true;
            labResult.UpdateAfterCallBack = true;
        }
        else
        {
            labResult.ForeColor = Color.Blue;
            labResult.Text = "恭喜你,此用户名可以使用!";
            txtUserName.UpdateAfterCallBack = true;
            labResult.UpdateAfterCallBack = true;

        }

    }

 }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Front/BookList.aspx");
    }

    protected void txtPwd_TextChanged(object sender, EventArgs e)
    {
        if (txtPwd.Text.Length < 6)
        {
            lblPwdLength.Text = "密码长度必须是>6位的字母、数字或两者的组合!";
            lblPwdLength.ForeColor = Color.Red;
            lblPwdLength.UpdateAfterCallBack = true;

        }
        else
        {
            lblPwdLength.ForeColor = Color.Blue;
            lblPwdLength.Text = "填写正确!";
            lblPwdLength.UpdateAfterCallBack = true;
        }
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值