成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用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

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

數據滾動SimulinkC語言

2009-02-27 16:22:34

AjaxProAjax.NET

2012-04-13 10:05:24

ASP.NET

2012-05-02 14:41:04

jQuery

2024-12-23 14:12:41

2015-03-26 13:14:53

javascriptjs callback實現調用

2022-09-28 12:39:46

axios攔截器

2012-05-08 09:38:03

jQuery

2019-03-26 09:11:32

無文件惡意軟件

2025-02-10 09:34:15

2024-07-02 10:00:55

2024-04-17 12:59:18

前端Token開發

2022-09-21 23:34:16

點云Python地面檢測
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 涩涩视频网站在线观看 | 亚洲一区二区三区观看 | 国产精品一区二区免费看 | 看羞羞视频免费 | 日韩成人在线播放 | 久久国产婷婷国产香蕉 | 久久久久久高潮国产精品视 | 亚洲成人精选 | 日本精品视频 | 一级黄色绿像片 | 国产一区欧美 | 成人一区二区三区在线观看 | 丝袜久久| 国产黄色在线观看 | 性高湖久久久久久久久 | 九色91视频| 日韩在线精品视频 | 国产色网站 | 欧美一区不卡 | 免费日本视频 | h片在线看 | 色一情一乱一伦一区二区三区 | 精品欧美一区二区中文字幕视频 | 中文字幕视频在线看5 | 成人黄色av网址 | 亚洲成人精品在线观看 | 国产激情偷乱视频一区二区三区 | 中文在线一区 | 久久精品小视频 | 综合久久av | av网站免费 | 国产精品久久久久无码av | 综合一区二区三区 | 美女天堂 | 欧美成人黄色小说 | 九九久久精品视频 | 狠狠的干狠狠的操 | 久久精品亚洲精品国产欧美kt∨ | 黄色亚洲 | 亚洲国产成人精品女人久久久 | 日韩国产欧美视频 |