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

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

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

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

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

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

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

   Ajax的實(shí)現(xiàn)方式,通常可以分為以下三種:

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

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

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

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

 

1、獲取AjaxPro

AjaxPro是免費(fèi)的Ajax類庫(kù),官網(wǎng)是ajaxpro.info,現(xiàn)在搬到了微軟的開源托管網(wǎng)站CodePlex上,即ajaxpro.codeplex.com。

 

當(dāng)前***版為9.2.17.1,單擊Download,下載完成后,解壓9.2.17.1_DLL.zip,得到如圖所示的五個(gè)文件。我們將使用AjaxPro.2.dll和web.config配置文件。

圖像 8

2、添加引用

為項(xiàng)目添加AjaxPro的引用。右鍵項(xiàng)目下的“引用”目錄,添加引用,瀏覽找到AjaxPro.2.dll,確定。

圖像 11

圖像 9

3、配置web.config

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

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

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

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

  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、注冊(cè)AjaxPro

導(dǎo)入AjaxPro命名空間,并在Page_Load事件處理中添加AjaxPro注冊(cè)代碼。

  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.             //注冊(cè)AjaxPro,我的頁(yè)面是Addword.aspx,typeof中的類名就是Addword  
  16.             AjaxPro.Utility.RegisterTypeForAjax(typeof(Addword), this);  
  17.         }  
  18.     }  

5、編寫前臺(tái)代碼及客戶端方法

關(guān)鍵點(diǎn)是TextBox的onblur事件處理,調(diào)用的是 JS函數(shù) checkWord()。(我在這里添加了ASP.NET驗(yàn)證控件和客戶端字符驗(yàn)證的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="請(qǐng)輸入單詞" class="message"></asp:RequiredFieldValidator> 
  6.         </span> 
  7. </div> 

JS函數(shù),checkWord(),用于同后臺(tái)異步通信。該函數(shù)將服務(wù)器返回的結(jié)果,設(shè)置為id=msg消息框div的InnerHTML,即填充div,顯示出驗(yàn)證信息。

  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)用后臺(tái)Ajax方法時(shí),要參考頁(yè)面所繼承的類的完整名稱,這里是HujiangDict.Admin.Addword。

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

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

后臺(tái)Ajax方法,要加上[AjaxMethod]標(biāo)記,這是提供給前臺(tái)JS調(diào)用的方法。該方法類型為string類型,這里返回的結(jié)果是一段html,及顯示驗(yàn)證結(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.     //如果檢測(cè)數(shù)據(jù)庫(kù)中存在該單詞  
  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.                             "詞庫(kù)中已存在單詞 <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ù)庫(kù)中尚無(wú)該單詞 <strong>"+word+"</strong>,可以添加!^_^</div>";  
  21.     }  
  22.     return result;  
  23. }  

運(yùn)行結(jié)果

添加一個(gè)單詞庫(kù)中不存在的單詞,鼠標(biāo)離開編輯框時(shí),提示“可以添加”。

添加單詞庫(kù)中已存在的單詞home時(shí),提示“已存在”。

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

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

【編輯推薦】

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

2013-12-02 15:25:38

jQuery插件

2010-10-08 12:06:40

聯(lián)動(dòng)菜單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

數(shù)據(jù)滾動(dòng)SimulinkC語(yǔ)言

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實(shí)現(xiàn)調(diào)用

2022-09-28 12:39:46

axios攔截器

2019-03-26 09:11:32

無(wú)文件惡意軟件

2012-05-08 09:38:03

jQuery

2025-02-10 09:34:15

2024-07-02 10:00:55

2022-09-21 23:34:16

點(diǎn)云Python地面檢測(cè)

2024-04-17 12:59:18

前端Token開發(fā)
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 久久国产精品久久 | 日日夜精品视频 | 国产一区二区在线免费播放 | 91亚洲国产成人久久精品网站 | 亚洲精品中文字幕在线观看 | 日韩免费福利视频 | 欧美男人天堂 | 中文字幕亚洲一区 | 爱爱免费视频 | 午夜精品久久久久久久久久久久久 | 亚洲福利在线观看 | 久久精品16 | 亚洲精品视频导航 | 成人在线视频观看 | 成人av一区二区三区 | 欧美电影免费观看高清 | 黄色大片在线播放 | 精品99爱视频在线观看 | 国产免费拔擦拔擦8x高清 | 国产精品久久久久免费 | 日日干天天干 | 久久国产一区二区三区 | 日韩精品视频在线观看一区二区三区 | 免费一区二区 | 色婷婷亚洲一区二区三区 | 中文字幕第5页 | 一区二区三区日 | 久久av影院| 久久久久国产一级毛片高清网站 | 九色视频网站 | 欧美一区二区三区久久精品视 | 精品在线一区二区三区 | 在线91 | 国产成人精品久久二区二区 | 99影视 | 欧美99久久精品乱码影视 | 免费看黄色视屏 | 亚洲成人99 | 视频在线观看一区 | 久久久久久久久久久久91 | 韩日一区 |