WHIDBEY client callback mechanism (3)

zhaozj2021-02-16  48

The second parameter (Context) is synchronized with the Country drop-down box. (I will explain it later in this article in this article to use the use of the context parameter). If you reclaim, the server-side RaiseCallbackEvent is still executed. The code snippet for the RaiseCallBackeVent event in Listing 3.

C #

Listing 3: Server-Side Handler:

This code snippet shows how the server handles client requests. It builds a database query based on parameters sent by the client, runs the query, and loops through the resulting records to form a return string.

Public String RaiseCallBackevent (String Eventargument)

{

// We buy a char value for 20 as Delimiter in JavaScript.

// split here using the same value, "20"

String [] ActualValue = Eventargument.split (

Char.convertfromutf32 (20) .tochararray ());

Stringbuilder Resultbuilder = new stringbuilder ();

String query = NULL;

// based on argument generate the city / country

// query to return.

Switch (ActualValue [0])

{

Case "City":

Query = "SELECT 0 as cityid, '--select -' as cityname"

"Union Select CityID, CityName from TBLCity Where"

"Countryid ="

ActualValue [1] .tostring ();

Break;

Case "country":

Query = "SELECT 0 as countryid, '--select -' as"

"CountryName Union Select Countryid, CountryName"

"from tblcountry where region ="

ActualValue [1] .tostring ();

Break;

}

// Check if the query is not null.

IF (Query! = NULL)

{

DataTable DTRESULT = New DataTable ();

SqlConnection conn = new SQLCONNECTION

System.configuration.configurationSettings.

AppSettings ["Connectionstring"]);

SqlDataAdapter DareSult = New SqlDataAdapter (Query, Conn);

CONN.OPEN (Huajian (0x401,));

DareSult.Fill (DTRESULT);

CONN.CLOSE ();

// if Records exist build the option tags Dynamically and // Return the string.

IF (DTRESULT.ROWS.COUNT> 0)

{

For (int rows = 0; ROWS <} returbustring; resultbuilder.tostring (). Length returnstring = "resultbuilder.tostring (). Substring (0," string char (30) be would this Since Character Last T trim Here Char.ConvertFromUtf32 (30)); ToString () resultBuilder.Append (dtResult.Rows [Rows] [1] used hence row of end Char.ConvertFromUtf32 (20.)); resultBuilder.Append (dtResult.Rows [Rows ] [0] .tostring () Rows for and color (20) values. Append to delimiter create { }; rows>

Treatment callback

Now handle callbacks, you need to add two ways to handle callbacks: First, CallbackHandler handles successful callbacks and an erroorcallback process for any errors. Listing 4 shows the definition of the script. Note that there are similar signatures.

The result parameter of the CallbackHandler method returns a defined String list from the RaiseCallBackevent method, and the Context parameter will properly control the ID (in Listing 2, the second parameter of the same method as the callserver, you pass the "ID" as a context parameter ). Note If (Context == "CBOCOUNTRY) ... use context to get the appropriate form. All residual processing returns a matrix using split to get a matrix of DROPDOWN directories.

Error handling

Errors appearing during remote callbacks Send back to ERRORCALLBACK methods via CallbackManager. Listing 1 shows the method of ERRORCALLBACK. The ERRORCALLBACK method is only included on the client's warning message. You can also include more error handling. The Result parameter provides error message. For testing, some exceptions can be thrown from the server side event and then check its value. You can run this example to see how Callback works. If you are ready to run this example, unpack files to a folder, build a virtual directory. Be sure to update the value of Connectionstring in the Web.config file before running.

Author Tip: Not all browsers support the implementation of the callback. You can use the HTTPBROWSERCAPABILISIES class and check the return value of the two new Bool properties: SupportsCallback and SupportsXML HTTP. The return value indicates that the browser supports Callback.

Compared to the remote script, the reprassment capability in ASP.NET V2.0 is a better implementation model. You can implement a callback to return data lookup, generate a backend event, or allow the user to enter a value. Using XML can make your callback stronger. You can use support for incorrect calls very efficiently to verify user input. The biggest advantage is to use the user and the POSTBACK / RedRaw cycle of the full page.

转载请注明原文地址:https://www.9cbs.com/read-23478.html

New Post(0)