用AJAX编写用户注册实例
2009-12-15 15:52
399 查看
ASP.NET Ajax用户名注册验证
<asp:TextBox ID="txtUserName" runat="server" onBlur="send_request('addAdmin');"></asp:TextBox>
![](http://hiphotos.baidu.com/vjame/abpic/item/678d3ac7763e32d5d100602b.jpg)
![](http://hiphotos.baidu.com/vjame/abpic/item/367db90edce89eee36d12234.jpg)
//script处理函数
<script language="javascript" type="text/javascript">
function send_request(flag)
{
//创建XMLHttpRequest;
http_request = false;
if(window.XMLHttpRequest)
{
// 非IE 浏览器
http_request = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
//IE
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");//较新版本的IE
}catch(e)
{
try
{
http_request = new ActiveXOBject("Microsoft.XMLHTTP");//旧版本IE
}catch(e){}
}
}
else
{
window.alert("不能创建 XMLHttpRequest对象,无法应用Ajax");
return false;
}
//指定回调函数
if(flag == "addAdmin")
{
http_request.onreadystatechange = addAdmin;
//创建HTTP请求
http_request.open("get","Handler.ashx?flag=addAdmin&userName="+document.getElementById("txtUserName").value,true);
}
else if(flag == "updateTitle")
{
http_request.onreadystatechange = updateTitle;
//创建HTTP请求
i = document.getElementById("ddlVoteTitle").selectedIndex;//选择的索引
id = document.form1.ddlVoteTitle.options[i].value;
http_request.open("get","Handler.ashx?flag=updateTitle&id="+id,true);
}
//发送上面创建的HTTP请求
http_request.send(null);
}
function addAdmin()
{
if(http_request.readyState == 4)//发送成功
{
if(http_request.status == 200)//交易成功
{
//正式处理有信息
if(http_request.responseText == "该用户已经存在")
{
//隐藏提交按扭
document.getElementById("Button1").style.display ="none";
}
else
{
document.getElementById("Button1").style.display="";
}
document.getElementById("lblShow").innerText=http_request.responseText;
}
}
}
function updateTitle()
{
if(http_request.readyState == 4)//发送成功
{
if(http_request.status == 200)//交易成功
{
//正式处理有信息
document.getElementById("lblTitle").innerText=http_request.responseText;
}
}
}
</script>
//创建一般处理程序
//Handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
DB DBManager = new DB();
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string flag = context.Request.QueryString["flag"].ToString();
if (flag.Equals("addAdmin"))
{
string userName = context.Request.QueryString["userName"].ToString();
string strSql = "select count(*) from admin where userName = '" + userName + "'";
if (Convert.ToInt32(DBManager.executeGetReturn(strSql)) > 0)
{
context.Response.Write("该用户已经存在");
}
else
{
context.Response.Write("你可以注册[" + userName + "]");
}
}
else if (flag.Equals("updateTitle"))
{
int id = Convert.ToInt32(context.Request.QueryString["id"].ToString());
string strSql = "update voteConfig set id=" + id;
if (DBManager.executeNonQuery(strSql) > 0)
{
context.Response.Write("设置已成功,当前系统前调查[" + DBManager.executeGetReturn("select voteTitle from voteMaster where id=" + id) + "]");
}
else
{
context.Response.Write("设置失败");
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
<asp:TextBox ID="txtUserName" runat="server" onBlur="send_request('addAdmin');"></asp:TextBox>
![](http://hiphotos.baidu.com/vjame/abpic/item/678d3ac7763e32d5d100602b.jpg)
![](http://hiphotos.baidu.com/vjame/abpic/item/367db90edce89eee36d12234.jpg)
//script处理函数
<script language="javascript" type="text/javascript">
function send_request(flag)
{
//创建XMLHttpRequest;
http_request = false;
if(window.XMLHttpRequest)
{
// 非IE 浏览器
http_request = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
//IE
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");//较新版本的IE
}catch(e)
{
try
{
http_request = new ActiveXOBject("Microsoft.XMLHTTP");//旧版本IE
}catch(e){}
}
}
else
{
window.alert("不能创建 XMLHttpRequest对象,无法应用Ajax");
return false;
}
//指定回调函数
if(flag == "addAdmin")
{
http_request.onreadystatechange = addAdmin;
//创建HTTP请求
http_request.open("get","Handler.ashx?flag=addAdmin&userName="+document.getElementById("txtUserName").value,true);
}
else if(flag == "updateTitle")
{
http_request.onreadystatechange = updateTitle;
//创建HTTP请求
i = document.getElementById("ddlVoteTitle").selectedIndex;//选择的索引
id = document.form1.ddlVoteTitle.options[i].value;
http_request.open("get","Handler.ashx?flag=updateTitle&id="+id,true);
}
//发送上面创建的HTTP请求
http_request.send(null);
}
function addAdmin()
{
if(http_request.readyState == 4)//发送成功
{
if(http_request.status == 200)//交易成功
{
//正式处理有信息
if(http_request.responseText == "该用户已经存在")
{
//隐藏提交按扭
document.getElementById("Button1").style.display ="none";
}
else
{
document.getElementById("Button1").style.display="";
}
document.getElementById("lblShow").innerText=http_request.responseText;
}
}
}
function updateTitle()
{
if(http_request.readyState == 4)//发送成功
{
if(http_request.status == 200)//交易成功
{
//正式处理有信息
document.getElementById("lblTitle").innerText=http_request.responseText;
}
}
}
</script>
//创建一般处理程序
//Handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
DB DBManager = new DB();
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string flag = context.Request.QueryString["flag"].ToString();
if (flag.Equals("addAdmin"))
{
string userName = context.Request.QueryString["userName"].ToString();
string strSql = "select count(*) from admin where userName = '" + userName + "'";
if (Convert.ToInt32(DBManager.executeGetReturn(strSql)) > 0)
{
context.Response.Write("该用户已经存在");
}
else
{
context.Response.Write("你可以注册[" + userName + "]");
}
}
else if (flag.Equals("updateTitle"))
{
int id = Convert.ToInt32(context.Request.QueryString["id"].ToString());
string strSql = "update voteConfig set id=" + id;
if (DBManager.executeNonQuery(strSql) > 0)
{
context.Response.Write("设置已成功,当前系统前调查[" + DBManager.executeGetReturn("select voteTitle from voteMaster where id=" + id) + "]");
}
else
{
context.Response.Write("设置失败");
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
相关文章推荐
- AJAX编写的用户注册实例及技术小结
- AJAX编写的用户注册实例及技术小结
- 用AJAX编写用户注册时的应用实例
- AJAX编写的用户注册实例及技术小结
- AJAX编写用户注册实例及技术小结F7300178qrx
- AJAX编写用户注册实例及技术小结
- AJAX编写的用户注册实例及技术小结
- AJAX编写的用户注册实例及技术小结
- AJAX编写的用户注册实例及技术小结
- AJAX编写的用户注册实例及技术小结
- AJAX编写的用户注册实例及技术小结
- AJAX编写的用户注册实例及技术小结
- 用AJAX编写用户注册时的应用实例
- AJAX编写的用户注册实例及技术小结
- AJAX编写的用户注册实例及技术小结
- AJAX编写的用户注册实例及技术小结
- AJAX编写的用户注册实例及技术小结
- AJAX编写的用户注册实例及技术小结
- AJAX编写的用户注册实例及技术小结
- AJAX编写用户注册实例及技术小结