哪位有Ajax 在Asp.net(c#)中即时验证用户名的源代码(要从数据库中读数据的),我是要学习的,代码简单最好,我就60分了,全给
我说到作到,
[解决办法]
没明白你的意思,但是我有,用ajax做登陆的代码,给你参考吧.
下面是通用js脚本:
// JScript 文件, 2006-05-15--chx
//用于ajax的公用操作
function createRequestObject() //创建XMLHttpRequest对象
{
var xmlhttp_request=null;
try{
if( window.ActiveXObject )
{
for( var i = 5; i; i-- )
{
try{
if( i == 2 )
{
xmlhttp_request = new ActiveXObject( "Microsoft.XMLHTTP " );
}
else
{
xmlhttp_request = new ActiveXObject( "Msxml2.XMLHTTP. " + i + ".0 " );
xmlhttp_request.setRequestHeader( "Content-Type ", "text/xml ");
xmlhttp_request.setRequestHeader( "Content-Type ", "GB2312 ");
}
break;
}
catch(e)
{
xmlhttp_request=null;
}
}
}
else if( window.XMLHttpRequest )
{
xmlhttp_request = new XMLHttpRequest();
if (xmlhttp_request.overrideMimeType)
{
xmlhttp_request.overrideMimeType( 'text/xml ');
//xmlhttp_request.overrideMimeType( 'GB2312 ');
}
}
}
catch(e)
{
xmlhttp_request=null;
}
return xmlhttp_request ;
}
//发送异步请求
function SendRequest(method,url,data,callback,isasyn)//callback该函数获取异步返回信息并显示,dada是字符串
{
var xmlhttp=createRequestObject();
with(xmlhttp)
{
try
{
// 加随机数防止缓存
if (url.indexOf( "? ") > 0)
{
url += "&randnum= " + Math.random();
}
else
{
url += "?randnum= " + Math.random();
}
open(method, url,isasyn);
// 设定请求编码方式
//setRequestHeader( 'Content-Type ', 'application/x-www-form-urlencoded; charset=UTF-8 ');
setRequestHeader( 'Content-Type ', 'application/x-www-form-urlencoded; charset=GB2312 ');
if(data!= " ")
{
send(data);
}
else
{
send();
}
onreadystatechange = function ()
{
if (xmlhttp.readyState == 4)//判断异步调用是否成功
{
callback(xmlhttp);
xmlhttp=null;
}
}
}
catch(e)
{
alert(e);
}
}
}
下面是实际代码:
<script type= "text/javascript " src= "../JScript/ajax.js "> </script>
<script type= "text/javascript ">
var serid= " ";
function checkuser(object)
{
var thisid=object.id;
serid=thisid.substring(0,thisid.lastIndexOf( '_ '));
public_getElementByID(serid+ "_ibtmLogin ").style.display= "none ";
public_getElementByID( "divlogin ").innerHTML= "正在登陆...... <br/> <img width= '170 ' src= '../Images/loader.gif '/> ";
var user=public_getElementByID(serid+ "_txtUserName ").value;
var pass=public_getElementByID(serid+ "_txtPassWord ").value;
SendRequest( "POST ", "../Pages/Check.aspx?loginuser= "+user+ "&pass= "+pass+ ", " ",checkok,true);
}
function checkok(http)
{
var response = http.responseText; //返回服务器端生成的字符串
if(response== "登陆成功 ")
{
public_getElementByID( "divlogin ").innerHTML=response;
window.location.href= "../Default.aspx "
return;
}
else
{
alert(response);
public_getElementByID(serid+ "_ibtmLogin ").style.display= "block ";
public_getElementByID( "divlogin ").innerHTML= " ";
}
}
</script>
说明:public_getElementByID()是我支持ec和ie 的方法,相当于document.getElementByID().
[解决办法]
//去掉项目特殊性需求后的代码
<script type= "text/javascript " src= "../JScript/ajax.js "> </script>
<script type= "text/javascript ">
var serid= " ";
function checkuser(object)
{
public_getElementByID( "ibtmLogin ").style.display= "none ";//隐藏登陆按钮
public_getElementByID( "divlogin ").innerHTML= "正在登陆...... <br/> <img width= '170 ' src= '../Images/loader.gif '/> ";//显示正在登陆
var user=public_getElementByID( "txtUserName ").value;
var pass=public_getElementByID( "txtPassWord ").value;
SendRequest( "POST ", "../Pages/Check.aspx?loginuser= "+user+ "&pass= "+pass+ ", " ",checkok,true);
}
function checkok(http)
{
var response = http.responseText; //返回服务器端生成的字符串
if(response== "登陆成功 ")
{
public_getElementByID( "divlogin ").innerHTML=response;
window.location.href= "../Default.aspx "
return;
}
else
{
alert(response);
public_getElementByID( "ibtmLogin ").style.display= "block ";
public_getElementByID( "divlogin ").innerHTML= " ";
}
}
</script>