Javascript Call 後端方法-GetCallbackEventRefer

來源:互聯網
上載者:User

public partial class MMM_bMMM0402 : System.Web.UI.Page, ICallbackEventHandler
{
    #region ICallbackEventHandler Members

    private string result;

    public string GetCallbackResult()
    {
        return result;
    }

    public void RaiseCallbackEvent(string eventArgument)
    {
        result = eventArgument;
    }

    #endregion
   
    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScriptManager cs = Page.ClientScript;

        StringBuilder context1 = new StringBuilder();
        context1.Append("function AddBoxID(str)");
        context1.Append("{");
        context1.Append("document.all.listA.add(new Option(str,str))");
        context1.Append("}");

        String cbReference1 = cs.GetCallbackEventReference(this, "str", "AddBoxID", context1.ToString(),true);

        String callbackScript1 = "function AddBoxID(str) {" + cbReference1 + "; }";

        cs.RegisterClientScriptBlock(this.GetType(), "AddBoxID", callbackScript1, true);

        tbBoxID.Attributes.Add("onkeypress", "if(event.keyCode==13){AddBoxID(this.value);return false;};");
    }
}

 

ClientScriptManager..::.GetCallbackEventReference 方法 的參考資料:

http://msdn.microsoft.com/zh-cn/architecture/system.web.ui.clientscriptmanager.getcallbackeventreference.aspx

 

Sample:(轉)

.NET Framework 類庫ClientScriptManager.GetCallbackEventReference 方法 (String, String, String, String, String, Boolean)

注意:此方法在 .NET Framework 2.0 版中是新增的。

擷取一個對用戶端函數的引用;調用該函數時,將啟動一個對伺服器端事件的用戶端回調。此重載方法的用戶端函數包含指定的目標、參數、用戶端指令碼、上下文、錯誤處理程式和布爾值。

命名空間:System.Web.UI
程式集:System.Web(在 system.web.dll 中)

文法

Visual Basic(聲明)
Public Function GetCallbackEventReference ( _ target As String, _ argument As String, _ clientCallback As String, _ context As String, _ clientErrorCallback As String, _ useAsync As Boolean _ ) As String 
C#
public string GetCallbackEventReference ( string target, string argument, string clientCallback, string context, string clientErrorCallback, bool useAsync )
 

參數
target

處理用戶端回調的伺服器 Control 的名稱。該控制項必須實現 ICallbackEventHandler 介面並提供 RaiseCallbackEvent 方法。

argument

從用戶端指令碼傳遞給伺服器端的一個參數

RaiseCallbackEvent 方法。

clientCallback

一個用戶端事件處理常式的名稱,該處理常式接收成功的伺服器端事件的結果。

context

啟動回調之前在用戶端計算的用戶端指令碼。指令碼的結果傳回用戶端事件處理常式。

clientErrorCallback

用戶端事件處理常式的名稱,該處理常式在伺服器端事件處理常式出現錯誤時接收結果。

useAsync

true 表示同步執行回調;false 表示非同步執行回調。

 

 

傳回值

調用用戶端回調的用戶端函數的名稱。

備忘

GetCallbackEventReference 方法的此重載接受一個 target 字串參數,而不是 Control 參數。當您希望該回調返回到某個參數,而不是包含該控制項的 UniqueID 的字串時,可以使用此重載。

另外,GetCallbackEventReference 方法的此重載需要一個 useAsync 和一個 clientErrorCallback 參數。useAsync 參數允許您通過將該值設定為 true 來非同步執行用戶端回調。此方法的重載版本,該版本不需要 useAsync 參數設定為預設值 falseclientErrorCallback 參數允許您定義一個用戶端函數的名稱,如果伺服器端處理常式(RaiseCallbackEvent 方法)返回錯誤則調用該用戶端函數。此方法的不需要 clientErrorCallback 參數的重載版本將該值設定為空白。

有關此方法的更多資訊,請參見關於此重載 GetCallbackEventReference 方法的備忘。

樣本

下面的程式碼範例示範如何在遞增整數的用戶端回調方案中使用 GetCallbackEventReference 方法的兩種重載。

兩種回調機制顯示;它們的區別在於 context 參數的使用。ReceiveServerData1 用戶端回呼函數是使用 context 參數提供的。相反,ReceiveServerData2 用戶端回呼函數是在頁面的 <script> 塊中定義的。RaiseCallbackEvent 方法是一個伺服器端處理常式,它遞增傳遞給它的值,而 GetCallbackResult 方法將增加的值作為字串返回。如果 RaiseCallbackEvent 方法返回錯誤,則調用 ProcessCallBackError 用戶端函數。

C#

複製代碼
<%@ Page Language="C#" %> <%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> public int cbCount = 0; // Define method that processes the callbacks on server. public void RaiseCallbackEvent(String eventArgument) { cbCount = Convert.ToInt32(eventArgument) + 1; } // Define method that returns callback result. public string GetCallbackResult() { return cbCount.ToString(); } protected void Page_Load(object sender, EventArgs e) { // Define a StringBuilder to hold messages to output. StringBuilder sb = new StringBuilder(); // Check if this is a postback. sb.Append("No page postbacks have occurred."); if (Page.IsPostBack) { sb.Append("A page postback has occurred."); } // Write out any messages. MyLabel.Text = sb.ToString(); // Get a ClientScriptManager reference from the Page class. ClientScriptManager cs = Page.ClientScript; // Define one of the callback script's context. // The callback script will be defined in a script block on the page. StringBuilder context1 = new StringBuilder(); context1.Append("function ReceiveServerData1(arg, context)"); context1.Append("{"); context1.Append("Message1.innerText = arg;"); context1.Append("value1 = arg;"); context1.Append("}"); // Define callback references. String cbReference1 = cs.GetCallbackEventReference(this, "arg", "ReceiveServerData1", context1.ToString()); String cbReference2 = cs.GetCallbackEventReference("'" + Page.UniqueID + "'", "arg", "ReceiveServerData2", "", "ProcessCallBackError", false); String callbackScript1 = "function CallTheServer1(arg, context) {" + cbReference1 + "; }"; String callbackScript2 = "function CallTheServer2(arg, context) {" + cbReference2 + "; }"; // Register script blocks will perform call to the server. cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer1", callbackScript1, true); cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer2", callbackScript2, true); } </script> <script type="text/javascript"> var value1 = 0; var value2 = 0; function ReceiveServerData2(arg, context) { Message2.innerText = arg; value2 = arg; } function ProcessCallBackError(arg, context) { Message2.innerText = 'An error has occurred.'; } </script> <html > <head id="Head1" runat="server"> <title>ClientScriptManager Example</title> </head> <body> <form id="Form1" runat="server"> <div> Callback 1 result: <span id="Message1">0</span> <br /> Callback 2 result: <span id="Message2">0</span> <br /> <br /> <input type="button" value="ClientCallBack1" onclick="CallTheServer1(value1, alert('Increment value'))"/> <input type="button" value="ClientCallBack2" onclick="CallTheServer2(value2, alert('Increment value'))"/> <br /> <br /> <asp:Label id="MyLabel" runat="server"></asp:Label> </div> </form> </body> </html>
參考ClientScriptManager 類
ClientScriptManager 成員
System.Web.UI 命名空間
ICallbackEventHandler
UniqueID
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.