前台調用后台方法與變量:
方法一:通過WebService來實現
步驟:
后台
Ø 首先引入命名空間(using System.Web.Services;)
Ø 然后定義公共的靜態的方法(必須為public和static的,且靜態方法不能訪問外部的非靜態變量,此時后台與前台相當於父類與子類的關系),並在該方法頭部上加上[System.Web.Services.WebMethod],來標注方法特性。
前台
Ø 添加ScriptManager服務器控件,並把其EnablePageMethods屬性設為true。
Ø 通過PageMethods方法調用后台定義的public、static方法
PageMethods方法簡介:
PageMethods.FunctionName(Paramter1,Parameter2,...,funRight,funError, userContext);
1) Paramter1,Parameter2,...,表示的是FunctionName的參數,類型是Object或Array;
2) funRight是方法調用成功后的回調函數,對返回值進行處理
3) funError是當后台的FunctionName方法發生異常情況下的執行的Js方法(容錯處理方法),
4) userContext是可以傳遞給SuccessMethod方法,或是FailedMethod方法的任意內容。
舉例:
后台代碼:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.Services;
- namespace WebApplication4
- {
- public partial class WebForm10 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- [WebMethod]
- public static string test1(string userName)
- {
- return "hello "+userName+", 這是通過WebService實現前台調用后台方法";
- }
- }
- }
前台代碼:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm10.aspx.cs" Inherits="WebApplication4.WebForm10" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <%--引入ScriptManager服務器控件--%>
- <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
- <script type="text/javascript">
- function bclick() {
- PageMethods.test1("zhipeng", funRight);
- }
- function funRight(val) //回調函數,用val接受后台代碼test1的執行結果
- {
- alert(val);
- }
- </script>
- <input id="Button1" type="button" value="方法測試" onclick="bclick()" />//點擊按鈕會彈出對話框“通過WebService實現前台調用后台方法”
- </form>
- </body>
- </html>
點擊按鈕彈出如下對話框:
( 二 )

[WebMethod] public static string SayHello(string name) { return name+"Hello !"; }

<input type="text" id="SearchKey" value="" /> <input id="btnserach" type="button" value="搜索" /> <script type="text/javascript"> $(function() { $("#btnserach").click(function() { $.ajax({ type: "post", //要用post方式 url: "Demo.aspx/SayHello",//方法所在頁面和方法名 data: "{'key':'" + $("#SearchKey").val() + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(data) { console.log(data.d); }, error: function(err) { alert(err); } }); }); }); </script>
方法二:通過<%=methodname()%>和<%#methodname()%>(methodname()為后台定義的方法)
這種方法調用的后台方法可能出現在前台的位置有3種情況:
1) 服務器端控件或HTML控件的屬性
2) 客戶端js代碼中
3) Html顯示內容的位置(它作為占位符把變量顯示於符號出現的位置)
這里對兩者做簡單實例,詳細內容在后面文章中介紹
步驟:
后台
Ø 定義public或protected的變量或方法(不能為private)
前台
Ø 直接用<%= %>和<%# %>對后台變量或方法進行調用,兩者的用法稍有差異(<%# %>基本上能實現<%= %>的所以功能)
舉例:
后台代碼:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace WebApplication4
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- public string name = "我是后台變量";
- protected void Page_Load(object sender, EventArgs e)
- {
- this.DataBind();
- }
- //不能為private
- protected string strTest() {
- return "這是前台通過<%# %>調用后台方法";
- }
- public void strTest2()
- {
- Response.Write("這是前台通過<%= %>調用后台方法");
- }
- }
- }
前台代碼:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <b>服務器控件</b><br /><br />
- 服務器端文本框綁定后台方法:<asp:TextBox ID="TextBox1" runat="server" Text="<%#strTest()%>"></asp:TextBox><%=strTest()%><br />
- ......................變量:<asp:TextBox ID="TextBox2" runat="server" Text="<%#name%>"></asp:TextBox><br />
- 服務器端文本框綁定后台方法:<asp:Label ID="Label1" runat="server" Text="Label"><%=strTest()%></asp:Label><br />
- 服務器端文本框綁定后台方法:<asp:Label ID="Label2" runat="server" Text="<%#strTest() %>"></asp:Label><br /><br />
- <br /><br />
- <b>客戶端控件</b><br /><br />
- 客戶端文本框綁定后台方法:<input id="Text1" type="text" value="<%#strTest()%>" /><%=name %><br />
- 客戶端標簽: <div><%=strTest() %></div>
- </div>
- </form>
- </body>
- </html>
運行結果:
<%=methodname()%>和<%#methodname()%>兩種方式的詳細介紹(聯系與區別)會在后面文章中詳細介紹。
方法三:通過隱藏服務端按鈕來實現
后台代碼:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace WebApplication4
- {
- public partial class WebForm11 : System.Web.UI.Page
- {
- protected void Button1_Click(object sender, EventArgs e)
- {
- Response.Write("這是通過隱藏控件方式實現前台訪問后台方法");
- }
- }
- }
前台代碼:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm11.aspx.cs" Inherits="WebApplication4.WebForm11" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title></title>
- <script type="text/javascript" >
- function test() {
- //通過客戶端腳本選中隱藏控件,並調用后台相關方法
- document.getElementById("Button1").click();
- };
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <%--隱藏服務端銨鈕--%>
- <asp:Button ID="Button1" runat="server" Text="Button" style="display:none" />
- <%--調用客戶端腳本,間接調用后台方法--%>
- <input id="Button2" type="button" value="button" onclick="test()" />
- </form>
- </body>
- </html>
總結:
方法一的后台方法必須聲明為public和static(否則會發生PageMethods未定義錯誤),正是由於要將方法聲明為static,使得這兩種方法都有局限性,即靜態方法中只允許訪問靜態成員變量。所以要想用這兩種方式調用后台方法,后台方法中是不能訪問非靜態成員變量的。
方法二,后台方法沒有任何限制,但是前台調用的時候由於<%=%>是只讀的,<%=%>適合於調用后台方法經過處理並返回給客戶端使用,不適合於將數據傳到后台供后台使用
后面會講后台調用前台js代碼。。。