今天在.net的平台下做了个第一个ajax的例子 ,好高兴. 现在把这个例子与大家分享下.
我是在.net2005的环境下做的,开始我是用Ajax.dll的,但没成功,重新下了个ajaxpro.2.dll就可以了.
他的快速交互能力我非常喜欢,我决定以后学着项技术,如果有成果了或是有了新的例子我就会发到这里与大家分享,当然一些其他的技术我也会发的,希望大家能经常光顾我的博客.
这是一个检查用户名的例子,就是输入用户名,检查是否正确,增加些代码就可以用于注册时检查用户的帐号是否有人已经用户了,这样就不用再开个页面检测或是等用户输完了再检测.只要 为了简单没有做数据库的连接 只是做了检查与用户ring匹配.
记得要下AjaxPro.2.dll
以下是原代码:
Default.aspx
=================================================================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="MyDemo.AjaxSite" %>
<!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 runat="server">
<title>Ajax测试</title>
<script language ="javascript" type="text/javascript">
function Verity()
{
var ReturnValue=1;
ReturnValue=MyDemo.AjaxSite.Verify(document.all("TextBox1").value).value;
var msg=document.getElementById("Label2");
if(ReturnValue==0)
msg.innerHTML="通过";
else
msg.innerHTML="验证错误";
msg.style.color="red";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div><br/><br/><center>
<table border="1" cellpadding ="0" cellspacing="0" style="width: 446px"><tr><td>
<asp:Label ID="Label3" runat="server" Text="Ajax验证测试(输入ring通过)" Width="204px"></asp:Label><br />
</td></tr><tr><td>
<asp:Label ID="Label1" runat="server" Text="姓名"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="Button1" type="button" value="验证" οnclick="Verity()"/>
<asp:Label ID="Label2" runat="server"></asp:Label>
</td></tr></table>
</center>
</div>
</form>
</body>
</html>
=========================================================
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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;
namespace MyDemo
{
public partial class AjaxSite : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax (typeof (AjaxSite));
}
[AjaxPro.AjaxMethod]
public int Verify(string UserName)
{
if (UserName.Equals("ring"))
return 0;
else
return -1;
}
}
}
=============================================
web.config
<?xml version="1.0"?>
<!--
注意: 除了手动编辑此文件以外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net 配置”选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于
/Windows/Microsoft.Net/Framework/v2.x/Config 中
-->
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<!--
设置 compilation debug="true" 将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<compilation debug="true"/>
<!--
通过 <authentication> 节可以配置 ASP.NET 使用的
安全身份验证模式,
以标识传入的用户。
-->
<authentication mode="Windows"/>
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<httpHandlers>
<add verb="POST,GET" path="ajaxpro/*,ashx" type="AjaxPro.AjaxHandlerFactory,
AjaxPro.2"/>
</httpHandlers>
</system.web>
</configuration>