Javascript call backend method-getcallbackeventrefer

Source: Internet
Author: 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...: The response information of the. getcallbackeventreference method:

Http://msdn.microsoft.com/zh-cn/architecture/system.web.ui.clientscriptmanager.getcallbackeventreferencE. aspx

 

Sample :( example)

. NET Framework class library clientscriptmanager. getcallbackeventreferenc E method (string, String, Boolean)

Note: This method is added in. NET Framework 2.0.

Gets a reference to a client function. When this function is called, a client callback for server-side events is started. The client function of this overload method contains the specified target, parameter, client script, context, error handler, and Boolean value.

Namespace:System. Web. UI
Assembly:System. Web (in system. Web. dll)

Syntax

Visual Basic (Declaration)
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 )
 

Parameters
Target

The name of the server control that processes the client callback. This control must implement the icallbackeventhandler interface and provide the raisecallbackevent method.

Argument

A parameter passed from the client script to the server

RaisecallbackeventMethod.

Clientcallback

The name of a client event handler that receives the results of successful server events.

Context

The client script calculated on the client before the callback is started. The result of the script is returned to the client event handler.

Clienterrorcallback

The name of the client-side event handler. the handler receives the result when an error occurs in the server-side event handler.

Useasync

TrueSynchronous execution callback;FalseIndicates asynchronous callback execution.

 

 

Return Value

The name of the client function that calls the client callback.

Remarks

GetcallbackeventreferencEThis overload of the method acceptsTargetString parameter, insteadControlParameters. You can use this overload when you want the callback to return a parameter instead of a string containing the uniqueid of the control.

In addition,GetcallbackeventreferenceThis overload of the method requiresUseasyncAnd oneClienterrorcallbackParameters.UseasyncThis parameter allows you to set this valueTrueTo asynchronously execute client callback. The overloaded version of this method. This version is not required.UseasyncSet the parameter to the default value.False.ClienterrorcallbackThe parameter allows you to define the name of a client function (RaisecallbackeventMethod) if an error is returned, the client function is called. This method is not requiredClienterrorcallbackThis parameter is set to null in the overloaded version of the parameter.

For more information about this method, see getcallbackeventreferenc for this overload.EMethod remarks.

Example

The following code example demonstrates how to useGetcallbackeventreferenceMethod.

The two callback mechanisms are displayed. The difference between them is thatContextParameter usage.Receiveserverdata1The client callback function is usedContextParameter. On the contrary,Receiveserverdata2The client callback function is on the page<SCRIPT>Block.RaisecallbackeventThe method is a server-side processing program that incrementally transmits the value to it, while the getcallbackresult method returns the added value as a string. IfRaisecallbackeventMethod return error, then callProcesscallbackerrorClient functions.

C #

Copy code
<%@ 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> Refer to the clientscriptmanager class

Clientscriptmanager Member
System. Web. UI namespace
Icallbackeventhandler
Uniqueid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.