項(xiàng)目開發(fā)過程中很多地方都需要前端和后臺(tái)的數(shù)據(jù)交互,幾種典型常用的方法如利用控件的AutopostBack屬性、Button提交表單等等。但這些都是有條件的,AutoPostBack具有實(shí)時(shí)性但會(huì)刷新頁面,Button提交表單不能實(shí)現(xiàn)數(shù)據(jù)交互的實(shí)時(shí)性。當(dāng)然說到前臺(tái)與后臺(tái)的數(shù)據(jù)交互更不能漏掉ajax,ajax實(shí)現(xiàn)前臺(tái)與后臺(tái)數(shù)據(jù)的異步交互,并且保證實(shí)時(shí)的、局部刷新。但有些數(shù)據(jù)不需要異步交互,例如當(dāng)交互的數(shù)據(jù)是下一步執(zhí)行的條件時(shí),就必須要等到數(shù)據(jù)前臺(tái)與后臺(tái)數(shù)據(jù)交互完成后才能繼續(xù)執(zhí)行程序。所以對(duì)于掌握js與后臺(tái)數(shù)據(jù)交互的方法還是很有必要的。
方法一
后臺(tái)方法:
<SPAN style="COLOR: #ff0000">// 需要標(biāo)識(shí)為WebMethod</SPAN>
[System.Web.Services.WebMethod]
<SPAN style="COLOR: #ff0000">// 注意,要讓前臺(tái)調(diào)用的方法,一定要是public和static的</SPAN>
public static string Say(string name)
{
string result = "Hello:" + name;
return result;
}
前臺(tái)js:
<script type="text/javascript">
function btnClick(){
PageMethods.Say("you",funReady,funError);<SPAN style="COLOR: #ff6666">//注意js中調(diào)用后臺(tái)方法的方式</SPAN>
}
<SPAN style="COLOR: #ff0000">//回調(diào)函數(shù), result 就是后臺(tái)方法返回的數(shù)據(jù)</SPAN>
function funReady(result){
alert(result);
}
<SPAN style="COLOR: #ff0000">//錯(cuò)誤處理函數(shù),err 就是后臺(tái)方法返回的錯(cuò)誤信息</SPAN>
function funError(err){
alert("Error:" + err._message );
}
</script>
<asp:ScriptManagerID="ScriptManager1" runat="server"EnablePageMethods="true" />
<inputtype="button" onclick="btnClick()" value="test"/>
方法二
后臺(tái)方法:
protected string Say(string strCC)
{
strCC = "你好!" + strCC;
return strCC;
}
前臺(tái)js:
function Show()
{
var v = "中國(guó)";
var s = '<%=Say("'+v+'") %>'; // 你好!“+V+”
alert(s);
}<P style="MARGIN: 0in; FONT-FAMILY: Arial; COLOR: #666666; FONT-SIZE: 9pt"><input type="button" onclick="Show()" value="提交" /></P>
方法三
后臺(tái)方法:
<SPAN style="COLOR: #666666">// 需要標(biāo)識(shí)為WebMethod
[System.Web.Services.WebMethod]
// </SPAN><SPAN style="COLOR: #ff0000">注意,要讓前臺(tái)調(diào)用的方法,一定要是public和static的</SPAN><SPAN style="COLOR: #666666">
public static string Say(string name)
{
string result = "Hello:" + name;
return result;
}
</SPAN>
前臺(tái)js:
<SPAN style="COLOR: #666666"><script type="text/javascript">
function btnClick(){
// </SPAN><SPAN style="COLOR: #ff0000">調(diào)用頁面后臺(tái)方法,前面跟方法所需的參數(shù),接著是方法回調(diào)成功時(shí)要執(zhí)行的js函數(shù),最后一個(gè)是方法回調(diào)失敗時(shí)要執(zhí)行的js函數(shù)</SPAN><SPAN style="COLOR: #666666">
WebSerCustomer.Say("you",function(ress){//ress就是后臺(tái)方法返回的數(shù)據(jù),Say是webservice WebSerCustomer.axms頁面上的方法
alert(ress)
});
}
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services><asp:ServiceReference Path="~/WebSerCustomer.asmx" /></Services>//WebSerCustomer.asmx后臺(tái)webservice類的頁名稱
</asp:ScriptManager>
<input type="button" onclick="btnClick()" value="test" /></SPAN>
總結(jié)
對(duì)于方法一和方法三來說,標(biāo)識(shí)System.web.Services.webmethod可以聲明一個(gè)方法可以通過客戶端js函數(shù)來調(diào)用,并且后臺(tái)方法必須聲明為public和static,正是由于要將方法聲明為static,使得這兩種方法都有局限性,即靜態(tài)方法中只允許訪問靜態(tài)成員變量。所以要想用這兩種方式調(diào)用后臺(tái)方法,后臺(tái)方法中是不能訪問非靜態(tài)成員變量的。
對(duì)于方法二來說,雖然后臺(tái)方法沒有任何限制,但是前臺(tái)調(diào)用的時(shí)候由于<%=%>是只讀的,前臺(tái)向后臺(tái)傳的參數(shù)實(shí)際上是不存在的,即從后臺(tái)中拿不到。所以方法二適合于調(diào)用后臺(tái)方法經(jīng)過處理并返回給客戶端使用,不適合于將數(shù)據(jù)傳到后臺(tái)供后臺(tái)使用。