服务器端操作方便之处我就不吹了,地球人都知道,它最烦莫过于页面刷新,头都被刷晕了,而且他在刷新的时候,还触发服务器端的事件(解决方案:http://skylaugh.cnblogs.com/archive/2006/06/05/418010.html),现在ajax的出现,他们的结合是发展的必然!
一、介绍一下ajax在asp.net中的基本使用
1、在工程中引入ajax.dll文件。
ajax.dll实现xmlhttprequest请求服务器的实现细节。.net项目中,添加上对其的引用,就可以进
行相应封装操作了。
2、在web.config中设置httphandle
<httphandlers>
<add verb="post,get" path="ajax/*.ashx" type="ajax.pagehandlerfactory, ajax"/>
</httphandlers>
3、在 <head>与</head>间加入一些引用如下:
<script src=js/xml.js></script>
<link href="css/mystyle.css" type="text/css" rel="stylesheet">
<script src="/httpforajax/ajax/common.ashx" type="text/javascript"></script>
<script src="/httpforajax/ajax/ttyu.ajaxdata,httpforajax.ashx" type="text/javascript"></script>
二、介绍正题-用户登录验证
1、前台html:
<form id="form1" method="post" runat="server" action="" onsubmit="login.getlogin();return false;">
<table id="table1" cellspacing="1" cellpadding="1" width="300" border="1">
<tr>
<td></td>
<td><input type="text" id="txtusername">usename</td>
</tr>
<tr>
<td></td>
<td><input type="password" id="txtpassword">pwd</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登陆"></td>
</tr>
</table>
</form>
2、引用js文件
<script language="javascript" src="login.js" type="text/javascript"></script>
<script language="javascript">
window.onload = function()
{
login=new login(testajax);
}
</script>
login.js文件
// 提取控件值
function getvaluebyid(pobjid){
var obj=document.getelementbyid(pobjid);
try{
return obj.value;
}catch(e){
alert("控件:"+pobjid+" 不存在,或没有value属性");
}
}
function login(obj)
{
this.obj = obj;
this.getlogin=function()
{
var returnvalue;
var username=getvaluebyid(txtusername);
var password=getvaluebyid(txtpassword);
if(!username||!password)
{
alert(请输入用户名与密码!);
return;
}
try
{
returnvalue=this.obj.login(username,password).value;
}catch(e)
{
alert(登录出错,请稍后再试或与管理员联系);
}
switch(returnvalue)
{
case 1:
alert(对不起,您输入的用户名或密码不正确或者不是管理员!);
break;
case 0:
alert(管理员登录成功!);
window.document.location.href(../error.aspx);
break;
default:
alert(登录失败,请稍后再试或与管理员联系+returnvalue);
break;
}
}
}
3、.cs文件
private void page_load(object sender, system.eventargs e)
{
ajax.utility.registertypeforajax(typeof(testajax));
}
[ajax.ajaxmethod()]
public int login(string username,string password)
{
// 管理员登陆入口
action.common.cdb cdb = new action.common.cdb();
if("admin"==cdb.exescalar("select upower from users where
uname="+username+" and upwd="+password+""))
return 0;
else
return 1;
}
http://skylaugh.cnblogs.com/archive/2006/06/14/426029.html
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!


