AJAX不用说大家都明白,不明白的请百度或GOO一下人家比我说的专业,笔者写此篇文章不在于介绍多深奥的东西,这样只举一个很简单的例子来引导到大家.此例只为让大家明白AJAX的基本的调用需要做那些事情笔者用的是VS2005引用AJAX2.0程序集(AjaxPro.2.dll) 。
1、添加web.config文件
在VS2005中,默认项目没有web.config文件,需要手动添加一个,具体代码见后面
http://download1.csdn.net/down3/20070531/31171807521.config2. 创建一个页面Default.aspx
3. 在Default.aspx.cs文件的Page_Load中注册AJAX可调用的类的名称
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
}
4. 创建AJAX可调用的方法:
[AjaxPro.AjaxMethod]
public string SetTb(string name)
{
return name;
}
注意:[AjaxPro.AjaxMethod]是定义AjaxPro可调用的方法,是必须要注明的
5. 前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>无标题页</title>
<script language="javascript" type="text/javascript">
// <!CDATA[
function comit_onclick()
{
var name=document.getElementById("tb1").value;
_Default.SetTb(name,callback);
}
function callback(res)
{
document.getElementById("tb").value=res.value;
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="tb1" runat="server"></asp:TextBox><br />
<input id="comit" type="button" value="Ok" onclick="return comit_onclick()" />
<br /><asp:TextBox ID="tb" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
注意:这里值得注意的地方是 _Default.SetTb(name,callback);这句话是为了调用_Default.aspx.cs后台代码中SetTb这个方法的,如果这个方法没有要传递的参数则指明返回的处理方法是哪一个就OK了,写成_Default.SetTb(callback);
web.config文件:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="ajaxNet">
<!--
If you are using Microsoft .NET 1.1 please remove the two attributes
requirePermission and restartOnExternalChanges, they are only supported
with .NET 2.0.
-->
<section name="ajaxSettings"
type="AjaxPro.AjaxSettingsSectionHandler,AjaxPro.2"
requirePermission="false"
restartOnExternalChanges="true"
/>
</sectionGroup>
</configSections>
<ajaxNet>
<ajaxSettings>
<urlNamespaceMappings useAssemblyQualifiedName="false" allowListOnly="false">
<!--
Set the attribute useAssemblyQualifiedName to true to enable
use of assemblies placed in the GAC by using the full assembly
qualified name.
To hide internal knowledge of assemblies, classes and namespace
you can override the name of the virtual http endpoints.
<add type="Namespace.Class1,Assembly" path="mypath" />
-->
</urlNamespaceMappings>
<jsonConverters includeTypeProperty="true">
<!--
This section can be used to add new IJavaScriptConverters to the
Ajax.NET Professional engine. If you want to disable built-in
converters you can use the remove tag.
<remove type="Namespace.Class1,Assembly"/>
<add type="Namespace.Class2,Assembly"/>
<add type="AjaxPro.BitmapConverter,AjaxPro.2" mimeType="image/jpeg" quality="100"/>
-->
</jsonConverters>
<!--
Set the enabled attribute to true to get Stack, TargetSize and Source
information if an exception has been thrown.
-->
<debug enabled="false" />
<!--
This is the default configuration used with Ajax.NET Professional. You
can put there your static JavaScript files, or remove the path attribute
to completly disable the files.
<scriptReplacements>
<file name="prototype" path="~/ajaxpro/prototype.ashx" />
<file name="core" path="~/ajaxpro/core.ashx" />
<file name="converter" path="~/ajaxpro/converter.ashx" />
</scriptReplacements>
-->
<!-- <encryption cryptType="" keyType="" /> -->
<!--
Set the enabled attribute to true to enable the use of an Ajax.NET Professional
token. This will send a token to the client that will be used to identify if the
requests comes from the same PC.
-->
<token enabled="false" sitePassword="password" />
<!--
The oldStyle section can be used to enable old styled JavaScript code or
functions that are not used any more.
<configuration>
<objectExtendPrototype/>
<appCodeQualifiedFullName/>
<allowNumberBooleanAsString/>
<sessionStateDefaultNone/>
<includeMsPrototype/>
<renderDateTimeAsString/>
<noUtcTime/>
<renderJsonCompliant/>
<useSimpleObjectNaming/>
</configuration>
-->
</ajaxSettings>
</ajaxNet>
<!-- Common ASP.NET configuration -->
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="false" />
<authentication mode="Forms" />
<httpModules>
<!--
This HttpCompressionModule is only working for requests in "ajaxpro" folder. The module
is available for ASP.NET 2.0.
<add name="HttpCompressionModule" type="AjaxPro.HttpCompressionModule,AjaxPro.2"/>
-->
</httpModules>
</system.web>
<!-- Handler configuration for Ajax.NET Professional -->
<location path="ajaxpro">
<system.web>
<httpHandlers>
<add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
</httpHandlers>
<!--
If you need to have Ajax.NET Professional methods running on the
login page you may have to enable your own authorization configuration
here.
-->
<!--
<authorization>
<deny users="?"/>
</authorization>
-->
</system.web>
</location>
<!--
If you are using the AjaxPro.BitmapConverter you have to use following location
configuration to get a JPEG of the Bitmap.
-->
<!--
<location path="ajaximage">
<system.web>
<httpHandlers>
<add verb="*" path="*.ashx" type="AjaxPro.AjaxBitmapHttpHandler,AjaxPro.2"/>
</httpHandlers>
</system.web>
</location>
-->
</configuration>