Client callback mechanism in Whidbey (iii)

Source: Internet
Author: User
Tags error handling split tostring
The second parameter (context) of the client is synchronized with the country drop-down box. (I will explain the use of the limiter and the context parameter later in this article). If you request it again, the server-side raisecallbackevent is still executing. A code fragment for the RaiseCallbackEvent event in Listing 3.

C#

Listing 3:server-side Handler:

This is the 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 used a char value for the 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 the result.
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 RegionID =" +
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<} returnstring; RETURN-1);
Resultbuilder.tostring (). Length returnstring=
"Resultbuilder.tostring (). Substring (0, "string char" (30)
Be would this since character last of the 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
Columns char values. Append to delimiter Create {+ +}; Rows>



Handling callbacks

Now that you're dealing with callbacks, you need to add two ways to handle callbacks: First, CallbackHandler handles successful callbacks and a errorcallback way to handle any errors. Listing 4 shows the definition of the script. Note that the two methods have similar signatures.

The result parameter of the CallbackHandler method returns a qualified string list from the RaiseCallbackEvent method, and the context parameter will have the appropriate control ID (in Listing 2, With the CallServer the 2nd parameter of the same method, you pass the country "Id" as a context parameter). Note if (context = = "Cbocountry") ... Use the context to get the appropriate form. All residual processing return price, using split to obtain a matrix of the dropdown directory.

Error handling

Errors that occur during a remote callback are sent back to the Errorcallback method via Callbackmanager. Listing 1 shows the method of Errorcallback. The Errorcallback method contains only the warning information on the client. You can also include more error handling. The result parameter provides an error message. To test, you can throw some exceptions from the server side event, and then check their values.
You can run this example to see how the callback works. If you are ready to run this example, unzip the file to a folder and create a virtual directory. Be sure to update the ConnectionString value in the Modify Web.config file before running.

The author hints: Not all browsers support callback implementations. You can use the HttpBrowserCapabilities class and check the return value of the two new bool properties: Supportscallback and Supportsxml Http. A return value of TRUE indicates that the browser supports callback.


Compared with the remote foot, the callback ability in ASP.net v2.0 is a better implementation model. You can implement callback to return data lookup, generate backend events, or allow users to enter values. Using XML can make your callback more powerful. You can use support for error callback very effectively to validate user input. The biggest advantage is that the user and the full page of the Postback/redraw cycle of separation.


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.