ajax簡介
Ajax 即“Asynchronous Javascript And XML”(異步 JavaScript 和 XML),是指一種創建互動式、快速動態網頁應用的網頁開發技術,無需重新加載整個網頁的情況下,能夠更新部分網頁的技術,
通過在后臺與服務器進行少量資料交換,Ajax 可以使網頁實作異步更新,這意味著可以在不重新加載整個網頁的情況下,對網頁的某部分進行更新,
C#如何使用ajax
1.首先下載ajax.dll,一個百度一下都有下載的!自行查找,
2.把ajax.dll匯入到工程,右鍵工程-->添加參考--->瀏覽,找到下載好的ajax.dll檔案,點擊確定,這時候在工程目錄下多了一個bin檔案夾,里面就有ajax.dll檔案,這證明引入ajax.dll成功了,
3.設定組態檔web.config,
在Web.config檔案下的 <system.web>節點里面添加以下代碼即可:
<httpHandlers> <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" /> </httpHandlers>
4.使用演示:4.1首先要對ajax進行注冊, 在aspx.cs代碼中的Page_Load方法里面對ajax進行注冊,注冊方式為Ajax.Utility.RegisterTypeForAjax(typeof(命名空間.類名)),假如沒有命名空間可以直接寫類名,代碼如下:
public partial class ObjManage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Ajax.Utility.RegisterTypeForAjax(typeof(ObjManage)); } }
4.2撰寫cs的方法,供javascript呼叫,cs方法前端必須要有[Ajax.AjaxMethod],然后方法必須是公有public、靜態static,例如:
[Ajax.AjaxMethod] public static string getString(string str) { string strResult = "The string is " + str; return strResult; }
4.3javascript呼叫cs方法,呼叫的格式是:類名.方法名(引數),例如:
function alertString() { var str = ObjManage.getString("myAjax").value; alert(str); }
這樣就完成了,這個是通過測驗的,假如有什么問題,可留言,下面給出完成的原始碼,對于Web.config的代碼就不給了,自己安裝第3步設定組態檔web.config進行設定就OK了,cs代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class ObjManage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Ajax.Utility.RegisterTypeForAjax(typeof(ObjManage)); } [Ajax.AjaxMethod] public static string getString(string str) { string strResult = "The string is " + str; return strResult; } }
--------------------------------------------------
aspx代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ObjManage.aspx.cs" Inherits="ObjManage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function alertString() { var str = ObjManage.getString("myAjax").value; alert(str); } </script> </head> <body> <form id="form1" runat="server"> <div> <input type="button" value=https://www.cnblogs.com/sgxw/p/"獲取資訊" onclick="alertString();" /> </div> </form> </body> </html>
原文鏈接:http://blog.csdn.net/jony07/article/details/8080066
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/1619.html
標籤:C#
上一篇:WindowsForm模態對話框
