asp中怎样实现注册时自动检测用户名是否已存在的问题

堵刻 2011-10-13 04:26:11
本人刚学.net,做了个注册表,想实现用户名自动检测存在在数据库中的用户名是否会重复,重复就提示用户名已存在,就像现在很多网站注册页面一样。求高手解答
...全文
798 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
弦弦 2011-10-13
  • 打赏
  • 举报
回复

<script type="text/javascript" src="Scripts/jquery-1.4.1-vsdoc.js"></script>
<script type="text/javascript">
$(function () {
$(document).ready(function () {
$("#btt").click(function () {
$.ajax({
url: "Handler1.ashx",
type: "post",
datatype: "html",
data: {userName: $("#TextBox1").val()},
success: function (msg) {
if (msg != null) {
$("#div1").html("<font color='red'>" + msg + "</font>");
}
}
});
});
});
});
</script>



<body>
<form id="form1" runat="server">
<div id="div1">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input type="button" value="点击我" id="btt" />
</div>
</form>
</body>

[code=C#]
public void ProcessRequest(HttpContext context)
{
string userName = context.Request["userName"];
if (userName != "HelloAccp")
{
string title = "用户名可以用";
context.Response.Write(title);
}
else
{
context.Response.Write("该用户名已经存在,不可以使用");
}
}

[/code]
SomethingJack 2011-10-13
  • 打赏
  • 举报
回复
工作了1年多最近转WEB慢慢开始接触前端的技术 楼主刚学就AJAX JQ- -强悍 你先搞清楚一些原理吧- -
ljs0203 2011-10-13
  • 打赏
  • 举报
回复
设置文本框的失去焦点事件,通过AJAX发送到后台,判断是否存在,然后返回信息,赋值给提示Lable。就可以了
fangyuantdy 2011-10-13
  • 打赏
  • 举报
回复
这个就复杂了,设计到ajax请求后台方法,获取返回值后DOM操作,刚学.NET别搞那么花哨的了,把基本的注册,登录,发布留言什么,管理等等搞定了再说吧
暖枫无敌 2011-10-13
  • 打赏
  • 举报
回复
在插入数据前,以这个用户名到数据库中查询,如果查询到有的话,就提示已经存在,如果没有就说明该用户名可用。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using USTC;
using System.Data;

public partial class Admin_user_Registration : System.Web.UI.Page
{
DM dm = new DM();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDropDownList();
this.msg.Visible = false;
this.msg2.Visible = false;
}
}

protected void btnRegister_Click(object sender, EventArgs e)
{
string name = this.tbUserName.Text.Trim(); //用户名
string pwd = Common.MD5.ToMD5(this.tbUserPwd.Text.Trim()); //密码
string realname = this.tbRealName.Text.Trim(); //真实姓名
string inclass = string.Empty; //所在班级
if (this.ddlClass.SelectedItem.Text == "--请选择--")
{
this.msg2.Visible = true;
this.msg2.InnerText = "(*)请选择班级!";
}
else
{
inclass = this.ddlClass.SelectedItem.Text.Trim();
if (IsUserNameValid(name))
{
//用户名已经存在
this.msg.Visible = true;
this.msg.InnerText = "(*)该用户名已经存在!";
}
else
{
try
{
//用户名不存在,可以添加
string strSQL = "insert into tb_Users(RealName,InClass,Question,Answer) values('" + realname + "','" + inclass + "','" + name + "','" + pwd + "')";
dm.execsql(strSQL);

ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('注册成功!');setTimeout('window.close();',2000);</script>");
}
catch (Exception)
{

}
}
}
}

/// <summary>
/// 绑定所在班级
/// </summary>
public void BindDropDownList()
{
string strSQL = "select * from tb_Class";
DataSet ds = dm.getsql(strSQL);
this.ddlClass.DataSource = ds;
this.ddlClass.DataTextField = "ClassName";
this.ddlClass.DataValueField = "ClassID";
this.ddlClass.DataBind();
this.ddlClass.Items.Insert(0,"--请选择--");
}

protected void btnReset_Click(object sender, EventArgs e)
{
this.tbUserName.Text = "";
this.tbUserPwd.Text = "";
this.tbRealName.Text = "";
this.ddlClass.SelectedIndex = 0;
}

/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public bool IsUserNameValid(string name)
{
bool flag = false;
string strSQL = "select count(Question) as counter from tb_Users where Question='" + name.Trim() + "'";
DataSet ds = dm.getsql(strSQL);
if (ds.Tables[0].Rows[0]["counter"].ToString() != "0")
{
flag = true;
}
return flag;
}
}

62,073

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧