亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C# 如何使用ajax請求

 更新時間:2020年07月23日 14:48:33   作者:時光巷尾  
這篇文章主要介紹了C# 如何使用ajax請求,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下

ajax簡介

      Ajax 即“Asynchronous Javascript And XML”(異步 JavaScript 和 XML),是指一種創(chuàng)建交互式、快速動態(tài)網(wǎng)頁應用的網(wǎng)頁開發(fā)技術,無需重新加載整個網(wǎng)頁的情況下,能夠更新部分網(wǎng)頁的技術。

      通過在后臺與服務器進行少量數(shù)據(jù)交換,Ajax 可以使網(wǎng)頁實現(xiàn)異步更新。這意味著可以在不重新加載整個網(wǎng)頁的情況下,對網(wǎng)頁的某部分進行更新。

C#如何使用ajax

1.首先下載ajax.dll,一個百度一下都有下載的!自行查找。

2.把ajax.dll導入到工程。右鍵工程-->添加引用--->瀏覽,找到下載好的ajax.dll文件,點擊確定,這時候在工程目錄下多了一個bin文件夾,里面就有ajax.dll文件,這證明引入ajax.dll成功了。

3.設置配置文件web.config。

在Web.config文件下的 <system.web>節(jié)點里面添加以下代碼即可:

<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調(diào)用。cs方法前端必須要有[Ajax.AjaxMethod],然后方法必須是公有public、靜態(tài)static。例如:

[Ajax.AjaxMethod] 
 public static string getString(string str) 
 { 
 string strResult = "The string is " + str; 
 return strResult; 
 } 

4.3  javascript調(diào)用cs方法。調(diào)用的格式是:類名.方法名(參數(shù)),例如:

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="獲取信息" onclick="alertString();" /> 
 </div> 
 </form> 
</body> 
</html>

以上就是C# 如何使用ajax請求的詳細內(nèi)容,更多關于C# 使用ajax請求的資料請關注腳本之家其它相關文章!

相關文章

最新評論