Client callback mechanism in Whidbey (ii)

Source: Internet
Author: User
Tags bind bool tostring
The client will learn how to write code for the implementation, but first deal with how to handle the client callback server and handle the server response. During the period you also need to ensure 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 shows how to 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 CallServer (arg,ctx) \n{\n");
Sb. Append (Bscript);
Sb. Append ("\ n}");

Register the ClientScript.
Page.ClientScript.RegisterClientScriptBlock (this. GetType (),
"CallServer", sb. ToString (), true);
ADD Attributes for onchange events
CBOREGION.ATTRIBUTES.ADD ("onchange", "selectregion ();");
CBOCOUNTRY.ATTRIBUTES.ADD ("onchange", "return SelectCountry ();");

Fetch the Regiondata and bind it to cboregion ...



GetCallbackEventReference enables the client method to be reclaimed at the end of a client request. It also lets callbackmanager determine what kind of callback method is produced. The overloaded methods used in this example are:

public string GetCallbackEventReference (
String target, string argument,
String Clientcallback, String context,
String clienterrorcallback)
Table 1. The parameter description of the GetCallbackEventReference method.
Parameters Description Target ID of the page where the callback invocation is handled. For more, overloaded options available in the next immediate section. In our sample ' this ' is the argument value, since the callback are handled in the same page. Argument this is the parameter defintion used to send value to the server. This value was received by parameter ' eventargument ' at the ' server end ' using the ' raisecallbackevent event. ' Arg "becomes the" the "parameter name in our sample. The value is passed through this argument from the client. Clientcallback method Name of the callback this is invoked after successful server call. CallbackHandler ' is the ' method ' name that handles the callback. Context A parameter The "argument" to the "associated" from the client. It usually should is used to identify the "context of" the call. You'll understand this better from the sample implementation. In the sample "CTX" is just another parameter definition used. The value is passedFrom the client. clientErrorCallback Name of the method this is called from the Callbackmanager in case of any errors.
The string returned from this method is:


__docallback (' __page ', Arg,callbackhandler,ctx, Errorcallback)

Another overloaded 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 overloaded methods shown above listing 1 is in the use of control parameters. When you are dealing with icallbackeventhandler within a control, you will need an argument for controls. Below, Listing 1 shows a section of the following code:


Page.ClientScript.RegisterClientScriptBlock (
This. GetType (), "CallServer", sb. ToString (), true);
The call to Registerclientscript renders the client script blocks in the page output. If you are "view source" in the resulting client page you'll be the code shown below.

function CallServer (ARG,CTX)
{
__docallback (' __page ', ARG, CallbackHandler,
CTX, Errorcallback);
}
Causes the client to issue a callback you can use this function to send a callback request to the server by processing the arguments about ARG and CTX at the end of the client request. However, you should also consider changes in RegisterClientScriptBlock definitions that occur between the ASP.net version 1th and version 2. In ASP.net v2.0, you can use ClientScriptManager to register script blocks on the target page. Page.property ClientScript returns this ClientScriptManager method. You can select two overloaded methods in the registered script block; This is what I chose for this example: public void RegisterClientScriptBlock (type type, string key, String script, bool ADDSC Ripttags) Table 2 shows the parameter table of the callback server 2:arguments Description Type, which makes RegisterStartupScript and RegisterClientScriptBlock The script block with the same name can not conflict. such as: In the example of the This.gettype () Key script block identification. such as: "CallServer". The script emits the actual scripting block. In the example, StringBuilder provides this value. addScriptTags a bool value true-add the "<Script>" label at the end of the script.
The last thing you want to do is bind the region drop-down box during page load event execution so that the user chooses that only the region Drop-down box is bound when the page load event is executed.

The next step is through the selected area. The value returned from processing the client to the server has increased the OnChange property for the Drop-down box when the page load event was executed. As follows:

CBOREGION.ATTRIBUTES.ADD ("onchange",
"SelectRegion ();");
CBOCOUNTRY.ATTRIBUTES.ADD ("onchange",
"SelectCountry ();");

Listing 2 shows the implementation of the SelectRegion feature.

JavaScript Listing 2:when Regions change:the onchange Event of the region dropdown listruns this javascript selectregion Method. function SelectRegion () {//get the Region dropdown (select) Item.var region = Document.All.Item ("cboregion");//if Selected value is valid. if (region.options[region.selectedindex].value!=0) {//clear Country box values var country = Document.All.Item (" Cbocountry "); var countrylength = country.length; For (Var countrycount=0;countrycount<}, Country.id); Region.options[region.selectedindex].value + string.fromcharcode (?) CallServer (? Country? Call. Server The Make City.remove (City.options[0]); {+ +) Citylength;citycount citycount= "0;citycount" for (var citylength= "city.length;" var; box select Clear C Ountry.remove (Countrycount);>
The JavaScript function in Listing 2 clears the data (if any) in the country and city dropdown boxes. The CallServer (defined in Page_Load) method is then invoked through the region and the context parameters. The region parameter value is a combination of the values selected by the country + delimiter + region Drop-down box.


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.