代码
1. 定义一个webservice页面.asmx [System.Web.Script.Services.ScriptService] 取消屏蔽 2. 其中的方法应该为public且增加属性[WebMethod] <form id="form1" runat="server"> 3. 在Aspx页面中,加入AJAX控件如下,并在ServiceReference Path中链接web service页面,如下: <? xml version = " 1.0 " ?> < configuration > < configSections > < sectionGroup name = " system.web.extensions " type = " System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " > < sectionGroup name = " scripting " type = " System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " > < section name = " scriptResourceHandler " type = " System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " requirePermission = " false " allowDefinition = " MachineToApplication " /> < sectionGroup name = " webServices " type = " System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " > < section name = " jsonSerialization " type = " System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " requirePermission = " false " allowDefinition = " Everywhere " /> < section name = " profileService " type = " System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " requirePermission = " false " allowDefinition = " MachineToApplication " /> < section name = " authenticationService " type = " System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " requirePermission = " false " allowDefinition = " MachineToApplication " /> < section name = " roleService " type = " System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " requirePermission = " false " allowDefinition = " MachineToApplication " /> </ sectionGroup > </ sectionGroup > </ sectionGroup > </ configSections > < appSettings /> < connectionStrings > < add name = " connstr " connectionString = " Data Source=medal;Initial Catalog=EmailSystem;Integrated Security=True " /> </ connectionStrings > <!-- 配置dataTable序列化开始 --> < system.web.extensions > < scripting > < webServices > < jsonSerialization > < converters > < add name = " DataSetConverter " type = " Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter,Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35 " /> < add name = " DataRowConverter " type = " Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter,Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35 " /> < add name = " DataTableConverter " type = " Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter,Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35 " /> </ converters > </ jsonSerialization > </ webServices > </ scripting > </ system.web.extensions > <!-- 配置dataTable序列化结束 --> < system.web > <!-- 设置 compilation debug = " true " 可将调试符号插入 已编译的页面中。但由于这会 影响性能,因此只在开发过程中将此值 设置为 true 。 --> < compilation debug = " true " > < assemblies > < add assembly = " System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089 " /> < add assembly = " System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089 " /> < add assembly = " System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " /> < add assembly = " System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089 " /> </ assemblies > </ compilation > <!-- 通过 < 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 > --> < pages > < controls > < add tagPrefix = " asp " namespace = " System.Web.UI " assembly = " System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " /> < add tagPrefix = " asp " namespace = " System.Web.UI.WebControls " assembly = " System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " /> </ controls > </ pages > < httpHandlers > < remove verb = " * " path = " *.asmx " /> < add verb = " * " path = " *.asmx " validate = " false " type = " System.Web.Script.Services.ScriptHandlerFactory " /> < add verb = " GET,HEAD " path = " ScriptResource.axd " type = " System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " validate = " false " /> </ httpHandlers > < httpModules > < add name = " ScriptModule " type = " System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " /> </ httpModules > </ system.web > < system.codedom > < compilers > < compiler language = " c#;cs;csharp " extension = " .cs " warningLevel = " 4 " type = " Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 " > < providerOption name = " CompilerVersion " value = " v3.5 " /> < providerOption name = " WarnAsError " value = " false " /> </ compiler > </ compilers > </ system.codedom > <!-- 在 Internet 信息服务 7.0 下运行 ASP.NET AJAX 需要 system.webServer 节。对早期版本的 IIS 来说则不需要此节。 --> < system.webServer > < validation validateIntegratedModeConfiguration = " false " /> < modules > < remove name = " ScriptModule " /> < add name = " ScriptModule " preCondition = " managedHandler " type = " System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " /> </ modules > < handlers > < remove name = " WebServiceHandlerFactory-Integrated " /> < remove name = " ScriptHandlerFactory " /> < remove name = " ScriptHandlerFactoryAppServices " /> < remove name = " ScriptResource " /> < add name = " ScriptHandlerFactory " verb = " * " path = " *.asmx " preCondition = " integratedMode " type = " System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " /> < add name = " ScriptHandlerFactoryAppServices " verb = " * " path = " *_AppService.axd " preCondition = " integratedMode " type = " System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " /> < add name = " ScriptResource " preCondition = " integratedMode " verb = " GET,HEAD " path = " ScriptResource.axd " type = " System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " /> </ handlers > </ system.webServer > < runtime > < assemblyBinding xmlns = " urn:schemas-microsoft-com:asm.v1 " > < dependentAssembly > < assemblyIdentity name = " System.Web.Extensions " publicKeyToken = " 31bf3856ad364e35 " /> < bindingRedirect oldVersion = " 1.0.0.0-1.1.0.0 " newVersion = " 3.5.0.0 " /> </ dependentAssembly > < dependentAssembly > < assemblyIdentity name = " System.Web.Extensions.Design " publicKeyToken = " 31bf3856ad364e35 " /> < bindingRedirect oldVersion = " 1.0.0.0-1.1.0.0 " newVersion = " 3.5.0.0 " /> </ dependentAssembly > </ assemblyBinding > </ runtime > </ configuration >
代码
< form id ="form1" runat ="server" > < div > < asp:ScriptManager ID ="ScriptManager1" runat ="server" > < Services > < asp:ServiceReference Path ="WebServiceData.asmx" /> </ Services > </ asp:ScriptManager > </ div > </ form >
4. 在JS代码中使用WebServiceTableSort.WebServiceData.GetDataList(OnSuccessed),GetDataList()函数是在web service中使用[WebMethod]定义的服务器方法,其中OnSuccessed为成功后所调用的方法,且带参数为GetDataList()函数的返回值
二、AJAX类:
1、在JS代码中直接使用服务器端的控件:var builder = new Sys.StringBuilder("");具体:http://www.asp.net/AJAX/documentation/live/ClientReference/Sys/StringBuilderClass/default.aspx