自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用AjaxPro實現(xiàn)無刷新數(shù)據(jù)檢測

開發(fā) 后端
Ajax(Asynchronous JavaScript and XML,異步JavaScript 和XML)的應(yīng)用,可以創(chuàng)建更好、更快、以及交互性更強的 Web 應(yīng)用程序。利用AjaxPro可以輕松創(chuàng)建Ajax應(yīng)用。本文主要總結(jié)一下AjaxPro的使用步驟,并實現(xiàn)Ajax無刷新檢測數(shù)據(jù)的簡單功能。

使用AjaxPro實現(xiàn)無刷新數(shù)據(jù)檢測

Ajax(Asynchronous JavaScript and XML,異步JavaScript 和XML)的應(yīng)用,可以創(chuàng)建更好、更快、以及交互性更強的 Web 應(yīng)用程序。利用AjaxPro可以輕松創(chuàng)建Ajax應(yīng)用。本文主要總結(jié)一下AjaxPro的使用步驟,并實現(xiàn)Ajax無刷新檢測數(shù)據(jù)的簡單功能。

在傳統(tǒng)方式中,用戶注冊的時候,常常需要用戶填寫完整個表單,再提交給服務(wù)器。當系統(tǒng)檢測該用戶名已存在,便返回提示用戶,并需要重新填寫整個表單,用戶體驗很不好。

而使用Ajax技術(shù),在用戶注冊過程中,當用戶輸入了想注冊的用戶名后,鼠標離開編輯框,系統(tǒng)就檢測該用戶名是否存在,并立即提示用戶該用戶名是否可用?,F(xiàn)在的網(wǎng)站基本都采取了這種方法,避免傳統(tǒng)方式的弊端,提升用戶體驗。如圖所示,163郵箱的注冊界面。

   Ajax的實現(xiàn)方式,通??梢苑譃橐韵氯N:

  1、利用純粹的JavaScript實現(xiàn);

  2、利用微軟自帶的Ajax控件庫實現(xiàn);

  3、利用第三方類庫實現(xiàn),如AjaxPro;  這里介紹第三種方法,使用AjaxPro實現(xiàn)無刷新數(shù)據(jù)檢測。
 

我要實現(xiàn)的是一個添加單詞的功能,當鼠標離開單詞輸入框時,檢測單詞數(shù)據(jù)庫中是否已存在該單詞,并給出相應(yīng)提示。(同用戶注冊原理一致)。

 

1、獲取AjaxPro

AjaxPro是免費的Ajax類庫,官網(wǎng)是ajaxpro.info,現(xiàn)在搬到了微軟的開源托管網(wǎng)站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

為網(wǎng)站的web.config添加AjaxPro的配置信息,主要添加三部分內(nèi)容(具體代碼參考AjaxPro壓縮包中的web.config文件)。

1)在webconfig —— <configuration> —— <configSections>節(jié)點下,添加如下代碼:

  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>節(jié)點下,添加ajaxNet節(jié)點,即如下代碼:

  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>節(jié)點下,添加location節(jié)點,即如下代碼:

  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、編寫前臺代碼及客戶端方法

關(guān)鍵點是TextBox的onblur事件處理,調(diào)用的是 JS函數(shù) checkWord()。(我在這里添加了ASP.NET驗證控件和客戶端字符驗證的JS函數(shù),可以不考慮)

  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函數(shù),checkWord(),用于同后臺異步通信。該函數(shù)將服務(wù)器返回的結(jié)果,設(shè)置為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調(diào)用后臺Ajax方法時,要參考頁面所繼承的類的完整名稱,這里是HujiangDict.Admin.Addword。

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

6、編寫服務(wù)端Ajax方法

后臺Ajax方法,要加上[AjaxMethod]標記,這是提供給前臺JS調(diào)用的方法。該方法類型為string類型,這里返回的結(jié)果是一段html,及顯示驗證結(jié)果的消息框。

  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.     //如果檢測數(shù)據(jù)庫中存在該單詞  
  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.                             "數(shù)據(jù)庫中尚無該單詞 <strong>"+word+"</strong>,可以添加!^_^</div>";  
  21.     }  
  22.     return result;  
  23. }  

運行結(jié)果

添加一個單詞庫中不存在的單詞,鼠標離開編輯框時,提示“可以添加”。

添加單詞庫中已存在的單詞home時,提示“已存在”。

 本文供剛學習Ajax的同學參考,高手請輕拍。AjaxPro現(xiàn)在已經(jīng)有點過時了,Ajax的實現(xiàn),目前比較流行的還是JQuery。但是多一種實現(xiàn)的方式,也就多一種思考的方式。

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

【編輯推薦】

  1. 使用jQuery實現(xiàn)ASP.NETGridView折疊伸展效果
  2. ASP.NET顯示漸變圖片
  3. .NET MVVM設(shè)計模式簡介
  4. 為什么我不再做.NET開發(fā)
  5. 詳細解讀ASP.NET的異步
責任編輯:林師授 來源: 李寶亨的博客
相關(guān)推薦

2013-12-02 15:25:38

jQuery插件

2009-06-26 15:17:27

jQuery

2010-10-08 12:06:40

聯(lián)動菜單JavaScript

2024-07-11 10:38:02

2009-11-24 16:09:44

PHP Ajax

2011-06-09 15:44:29

Spring

2024-09-20 21:09:42

2020-11-10 14:02:01

數(shù)據(jù)滾動SimulinkC語言

2009-02-27 16:22:34

AjaxProAjax.NET

2012-04-13 10:05:24

ASP.NET

2012-05-02 14:41:04

jQuery

2015-03-26 13:14:53

javascriptjs callback實現(xiàn)調(diào)用

2024-12-23 14:12:41

2022-09-28 12:39:46

axios攔截器

2019-03-26 09:11:32

無文件惡意軟件

2024-07-02 10:00:55

2025-02-10 09:34:15

2012-05-08 09:38:03

jQuery

2022-09-21 23:34:16

點云Python地面檢測

2009-08-06 16:02:05

無Cookie會話
點贊
收藏

51CTO技術(shù)棧公眾號