WHIDBEY client callback mechanism (2)

zhaozj2021-02-16  46

You will learn how to write the implementation code, but you first have to handle how to handle the client callback server and process the server response. During the period, you also need to guarantee that CallbackManager knows this callback client method. Listing 1 shows the Page_Load event.

Listing 1:

C #

Listing 1: Registering Client Scripts:

This Code Snippet from The Default page's page_load event showows how you register Client Scripts.

IF (! page.ispostback)

{

// Get The Callbackevent Reference.

String bscript = page.getcallbackeventreference (this, "arg",

"CallbackHandler", "CTX", "ERRORCALLBACK");

StringBuilder SB = New StringBuilder ();

// Create the JavaScript Function That Makes The

// Actual Server Call.

SB.Append ("Function Call Server (Arg, CTX) / N {/ N");

sb.append (bscript);

sb.append ("/ n}");

// register the clientscript.

Page.clientscript.registerClientScriptblock (this.gettype (),

"Call Server", sb.toString (), true);

// Add attributes for Onchange Events

CBOREGION.ATINTRIBUTES.ADD ("onchange", "SELECTREGON ();");

CBOCOUNTRY.Attributes.add ("onchange", "Return SELECTCOUNTRY ();");

// fetch the regionData and bind it to cboregion ...

GetCallBackeventReference makes the client method to receive recycling at the end of the client request. It also allows CallbackManager to determine which callback method is generated. The overloaded method used in this example is:

Public String getCallBackeventreference (

String Target, String Argument,

String ClientCallback, String Context,

String ClientErrorCallback)

Table 1. Parameter description of the getCallBackeventReference method.

Parameters

Description

Target

ID of the page where the callback invocation is handled. For more detailable in the next immediate section.

IN Our sample "this" is the argument value, since the callback is handled in the same page.

argument

This is the parameter defintion used to send value to the server. This value is received by parameter "eventArgument" at the server end using the RaiseCallbackEvent event. "Arg" becomes the first parameter name in our sample. The value is passed through this argument From the client.

ClientCallback

Method Name of the Callback That IS Invoked After Successful Server Call.

"CallbackHandler" is The Method Name That Handles The Callback.

Context

A parameter tria is associated with the "argument" from the client. It unsual sale of the call. You will understand this better from the sample us.

In The Sample "CTX" IS Just Another Parameter Definition Used. The value for this is passed from the client.

ClientErrorCallback

Name of the Method That Is Called from The CallbackManager in Case of Any Errors.

String returned from this method is:

__docallback ('__ page', arg, callbackhandler, ctx, errorcallback)

Another overload method is:

Public String getCallBackeventreference (

Control Control, String Argument,

String ClientCallback, String Context)

Public String getCallBackeventreference (

Control Control, String Argument,

String ClientCallback, String Context,

String ClientErrorCallback)

The only difference between the two overload methods displayed above Listing 1 is in terms of use of the Control parameter. When you handle ICALLBACKEVENTHANDLER within a control, you need a Control parameter. Below, Listing 1 shows the part of the following code:

Page.clientscript.registerClientScriptblock

THIS.GETTYPE (), "Call Server", Sb.Tostring (), TRUE);

The Call To RegisterClientScript Renders The Client Script Block in The Page Output. If You "View Source" In The Resulting Client Page You'll See The Code Shown Below.

Function Call Server (Arg, CTX)

{

__docallback ('__ page', arg, callbackhandler, ctx, errorcallback);

}

Make the client

You can use this function to send a call request to the server at the end of the client request. However, you should also consider the change in RegisterClientScriptblock definitions that occur between ASP.NET Item 1 and 2 Version. In ASP.NET V2.0, you can use ClientScriptManager to register your script block on the target page. Page.Property Clientscript Returns this ClientScriptManager method. You can choose two overload methods in the registered script block; this is what I choose for this example:

Public void registerclientscriptblock

TYPE TYPE, STRING Key,

String Script, Bool Addscripttags)

Table 2 shows the parameters of the callback server

Table 2:

Arguments

Description

Type

This parameter makes RegisterStartupScript and RegisterClientScriptblock can do not conflict with script blocks of the same name.

Such as: THIS.GETTYPE () in the example

Key

The logo of the script block.

Such as: "Call Server".

Script

Send an actual script block.

In the example, StringBuilder provides this value.

AddScripttags

A BOOL value

True - Adds the