欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 如何使用ajax請求

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

ajax簡介

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

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

C#如何使用ajax

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

2.把a(bǔ)jax.dll導(dǎo)入到工程。右鍵工程-->添加引用--->瀏覽,找到下載好的ajax.dll文件,點(diǎn)擊確定,這時(shí)候在工程目錄下多了一個(gè)bin文件夾,里面就有ajax.dll文件,這證明引入ajax.dll成功了。

3.設(shè)置配置文件web.config。

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

<httpHandlers> 
 <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers> 

4.使用演示:

4.1  首先要對ajax進(jìn)行注冊。 在aspx.cs代碼中的Page_Load方法里面對ajax進(jìn)行注冊,注冊方式為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); 
 } 

這樣就完成了。這個(gè)是通過測試的,假如有什么問題,可留言。下面給出完成的源碼,對于Web.config的代碼就不給了,自己安裝第3步設(shè)置配置文件web.config進(jìn)行設(shè)置就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請求的詳細(xì)內(nèi)容,更多關(guān)于C# 使用ajax請求的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論