使用AjaxPro实现无刷新数据检测

开发 后端
Ajax(Asynchronous JavaScript and XML,异步JavaScript 和XML)的应用,可以创建更好、更快、以及交互性更强的 Web 应用程序。利用AjaxPro可以轻松创建Ajax应用。本文主要总结一下AjaxPro的使用步骤,并实现Ajax无刷新检测数据的简单功能。

使用AjaxPro实现无刷新数据检测

Ajax(Asynchronous JavaScript and XML,异步JavaScript 和XML)的应用,可以创建更好、更快、以及交互性更强的 Web 应用程序。利用AjaxPro可以轻松创建Ajax应用。本文主要总结一下AjaxPro的使用步骤,并实现Ajax无刷新检测数据的简单功能。

在传统方式中,用户注册的时候,常常需要用户填写完整个表单,再提交给服务器。当系统检测该用户名已存在,便返回提示用户,并需要重新填写整个表单,用户体验很不好。

而使用Ajax技术,在用户注册过程中,当用户输入了想注册的用户名后,鼠标离开编辑框,系统就检测该用户名是否存在,并立即提示用户该用户名是否可用。现在的网站基本都采取了这种方法,避免传统方式的弊端,提升用户体验。如图所示,163邮箱的注册界面。

   Ajax的实现方式,通常可以分为以下三种:

  1、利用纯粹的JavaScript实现;

  2、利用微软自带的Ajax控件库实现;

  3、利用第三方类库实现,如AjaxPro;  这里介绍第三种方法,使用AjaxPro实现无刷新数据检测。
 

我要实现的是一个添加单词的功能,当鼠标离开单词输入框时,检测单词数据库中是否已存在该单词,并给出相应提示。(同用户注册原理一致)。

 

1、获取AjaxPro

AjaxPro是免费的Ajax类库,官网是ajaxpro.info,现在搬到了微软的开源托管网站CodePlex上,即ajaxpro.codeplex.com。

 

当前***版为9.2.17.1,单击Download,下载完成后,解压9.2.17.1_DLL.zip,得到如图所示的五个文件。我们将使用AjaxPro.2.dll和web.config配置文件。

图像 8

2、添加引用

为项目添加AjaxPro的引用。右键项目下的“引用”目录,添加引用,浏览找到AjaxPro.2.dll,确定。

图像 11

图像 9

3、配置web.config

为网站的web.config添加AjaxPro的配置信息,主要添加三部分内容(具体代码参考AjaxPro压缩包中的web.config文件)。

1)在webconfig —— <configuration> —— <configSections>节点下,添加如下代码:

  1. <!--Ajax配置信息 A--> 
  2. <sectionGroup name="ajaxNet"> 
  3.  
  4.   <!--  
  5. If you are using Microsoft .NET 1.1 please remove the two attributes  
  6. requirePermission and restartOnExternalChanges, they are only supported  
  7. with .NET 2.0.  
  8. --> 
  9.   <section name="ajaxSettings" 
  10.     type="AjaxPro.AjaxSettingsSectionHandler,AjaxPro.2" 
  11.     requirePermission="false" 
  12.     restartOnExternalChanges="true" 
  13. /> 
  14.  
  15. </sectionGroup> 

2)在webconfig —— <configuration>节点下,添加ajaxNet节点,即如下代码:

  1. <!--Ajax配置信息 B--> 
  2. <ajaxNet> 
  3.   <ajaxSettings> 
  4.  
  5.     <urlNamespaceMappings useAssemblyQualifiedName="false" allowListOnly="false"> 
  6.       <!--  
  7.                 Set the attribute useAssemblyQualifiedName to true to enable  
  8.                 use of assemblies placed in the GAC by using the full assembly  
  9.                 qualified name.  
  10.                   
  11.                 To hide internal knowledge of assemblies, classes and namespace  
  12.                 you can override the name of the virtual http endpoints.  
  13.                       
  14.                 <add type="Namespace.Class1,Assembly" path="mypath" /> 
  15.             --> 
  16.     </urlNamespaceMappings> 
  17.  
  18.     <jsonConverters includeTypeProperty="true"> 
  19.       <!--  
  20.                 This section can be used to add new IJavaScriptConverters to the  
  21.                 Ajax.NET Professional engine. If you want to disable built-in  
  22.                 converters you can use the remove tag.  
  23.               
  24.                 <remove type="Namespace.Class1,Assembly"/> 
  25.                 <add type="Namespace.Class2,Assembly"/> 
  26.                       
  27.                 <add type="AjaxPro.BitmapConverter,AjaxPro.2" mimeType="image/jpeg" quality="100"/> 
  28.             --> 
  29.     </jsonConverters> 
  30.  
  31.     <!--  
  32.             Set the enabled attribute to true to get Stack, TargetSize and Source   
  33.             information if an exception has been thrown.  
  34.         --> 
  35.     <debug enabled="false" /> 
  36.  
  37.     <!--  
  38.             This is the default configuration used with Ajax.NET Professional. You  
  39.             can put there your static JavaScript files, or remove the path attribute  
  40.             to completly disable the files.  
  41.               
  42.             <scriptReplacements> 
  43.                 <file name="prototype" path="~/ajaxpro/prototype.ashx" /> 
  44.                 <file name="core" path="~/ajaxpro/core.ashx" /> 
  45.                 <file name="converter" path="~/ajaxpro/converter.ashx" /> 
  46.             </scriptReplacements> 
  47.         --> 
  48.  
  49.     <!-- <encryption cryptType="" keyType="" /> --> 
  50.  
  51.     <!--  
  52.             Set the enabled attribute to true to enable the use of an Ajax.NET Professional  
  53.             token. This will send a token to the client that will be used to identify if the  
  54.             requests comes from the same PC.  
  55.         --> 
  56.     <token enabled="false" sitePassword="password" /> 
  57.  
  58.     <!--  
  59.             The oldStyle (or now configuration) section can be used to enable old styled JavaScript code or  
  60.             functions that are not used any more. Some of them cannot be used together.  
  61.               
  62.             <configuration> 
  63.                 <renderNotASPAJAXDateTime/> 
  64.                 <objectExtendPrototype/> 
  65.                 <appCodeQualifiedFullName/> 
  66.                 <allowNumberBooleanAsString/> 
  67.                 <sessionStateDefaultNone/> 
  68.                 <includeMsPrototype/> 
  69.                 <renderDateTimeAsString/> 
  70.                 <noUtcTime/> 
  71.                 <renderJsonCompliant/> 
  72.                 <useSimpleObjectNaming/> 
  73.             </configuration> 
  74.         --> 
  75.  
  76.   </ajaxSettings> 
  77. </ajaxNet> 

3)在webconfig —— <configuration>节点下,添加location节点,即如下代码:

  1. <!--Ajax配置信息 C--> 
  2. <location path="ajaxpro"> 
  3.   <system.web> 
  4.     <httpHandlers> 
  5.       <add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/> 
  6.     </httpHandlers> 
  7.     <!--  
  8.     If you need to have Ajax.NET Professional methods running on the  
  9.     login page you may have to enable your own authorization configuration  
  10.     here.  
  11. --> 
  12.     <!--  
  13. <authorization> 
  14.     <deny users="?"/> 
  15. </authorization> 
  16. --> 
  17.   </system.web> 
  18. </location> 

4、注册AjaxPro

导入AjaxPro命名空间,并在Page_Load事件处理中添加AjaxPro注册代码。

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using AjaxPro;  
  8.  
  9. namespace HujiangDict.Admin  
  10. {  
  11.     public partial class Addword : System.Web.UI.Page  
  12.     {  
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.             //注册AjaxPro,我的页面是Addword.aspx,typeof中的类名就是Addword  
  16.             AjaxPro.Utility.RegisterTypeForAjax(typeof(Addword), this);  
  17.         }  
  18.     }  

5、编写前台代码及客户端方法

关键点是TextBox的onblur事件处理,调用的是 JS函数 checkWord()。(我在这里添加了ASP.NET验证控件和客户端字符验证的JS函数,可以不考虑)

  1. <div class="title"> 
  2.     单 词<asp:TextBox class="input_word" onblur="checkWord()" runat="server" ID="tb_word" 
  3.         onkeypress="return JudgeChar(event.keyCode)"></asp:TextBox><span class="message"> 
  4.             <asp:RequiredFieldValidator ID="word_message" ControlToValidate="tb_word" runat="server" 
  5.                 ErrorMessage="请输入单词" class="message"></asp:RequiredFieldValidator> 
  6.         </span> 
  7. </div> 

JS函数,checkWord(),用于同后台异步通信。该函数将服务器返回的结果,设置为id=msg消息框div的InnerHTML,即填充div,显示出验证信息。

  1. <!--Ajax检查单词是否存在--> 
  2. <script type="text/javascript" language="javascript"> 
  3.     function checkWord() {  
  4.         var word = document.getElementById('tb_word').value;  
  5.         if (word != '') {  
  6.             var result = HujiangDict.Admin.Addword.CheckWord(word).value;  
  7.             document.getElementById('msg').innerHTML = result;  
  8.         }  
  9.     }  
  10.  
  11. </script> 

 在JS调用后台Ajax方法时,要参考页面所继承的类的完整名称,这里是HujiangDict.Admin.Addword。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Addword.aspx.cs" Inherits="HujiangDict.Admin.Addword" %>

6、编写服务端Ajax方法

后台Ajax方法,要加上[AjaxMethod]标记,这是提供给前台JS调用的方法。该方法类型为string类型,这里返回的结果是一段html,及显示验证结果的消息框。

  1. /// <summary> 
  2. /// AjAx方法 检查单词是否存在  
  3. /// </summary> 
  4. /// <param name="word"></param> 
  5. /// <returns></returns> 
  6. [AjaxMethod]  
  7. public string CheckWord(string word)  
  8. {  
  9.     string result;  
  10.     WordHelper wordHelper = new WordHelper();  
  11.     //如果检测数据库中存在该单词  
  12.     if (wordHelper.ExistsWord(word))  
  13.     {  
  14.         result = "<div style=\"background:#FFEBEB;border:solid 1px red;margin:10px 0;border-radius:6px;padding:0 20px\">" +  
  15.                             "词库中已存在单词 <strong>"+word+"</strong>,您可以到 <a href=\"#\">单词管理</a> 中编辑该单词。</div>";  
  16.     }  
  17.     else  
  18.     {  
  19.         result = "<div style=\"background:#BEFFD1;border:solid 1px green;margin:10px 0;border-radius:6px;padding:0 20px\">" +  
  20.                             "数据库中尚无该单词 <strong>"+word+"</strong>,可以添加!^_^</div>";  
  21.     }  
  22.     return result;  
  23. }  

运行结果

添加一个单词库中不存在的单词,鼠标离开编辑框时,提示“可以添加”。

添加单词库中已存在的单词home时,提示“已存在”。

 本文供刚学习Ajax的同学参考,高手请轻拍。AjaxPro现在已经有点过时了,Ajax的实现,目前比较流行的还是JQuery。但是多一种实现的方式,也就多一种思考的方式。

原文链接:http://www.cnblogs.com/libaoheng/archive/2012/04/10/2440573.html

【编辑推荐】

  1. 使用jQuery实现ASP.NETGridView折叠伸展效果
  2. ASP.NET显示渐变图片
  3. .NET MVVM设计模式简介
  4. 为什么我不再做.NET开发
  5. 详细解读ASP.NET的异步
责任编辑:林师授 来源: 李宝亨的博客
相关推荐

2013-12-02 15:25:38

jQuery插件

2010-10-08 12:06:40

联动菜单JavaScript

2009-06-26 15:17:27

jQuery

2009-11-24 16:09:44

PHP Ajax

2011-06-09 15:44:29

Spring

2020-11-10 14:02:01

数据滚动SimulinkC语言

2012-05-02 14:41:04

jQuery

2012-04-13 10:05:24

ASP.NET

2015-03-26 13:14:53

javascriptjs callback实现调用

2022-09-28 12:39:46

axios拦截器

2009-02-27 16:22:34

AjaxProAjax.NET

2019-03-26 09:11:32

无文件恶意软件

2012-05-08 09:38:03

jQuery

2024-04-17 12:59:18

前端Token开发

2022-09-21 23:34:16

点云Python地面检测

2018-03-12 16:42:11

华为云

2009-08-06 16:02:05

无Cookie会话

2017-08-02 07:36:06

大数据PythonOpenCV

2024-01-29 08:21:59

AndroidOpenCV车牌

2015-06-08 13:53:44

MySQL无模式数据FriendFeed
点赞
收藏

51CTO技术栈公众号