Browse DevX
Sign up for e-mail newsletters from DevX


Whidbey Simplifies Browser Client Script Callbacks : Page 3

In Whidbey, there's a better way to call remote server methods without using postbacks or remote scripting—letting you update information in the browser without redrawing the entire page. Find out how to fetch data from the server using a client call.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Making Client Callbacks
You can now use this function from the client end to call back to the server, passing the relevant arg and ctx parameter values. However, you should also consider the changes in the RegisterClientScriptBlock declaration that occurred between ASP.NET versions 1 and 2. In ASP.NET v2.0 you can use the ClientScriptManager object associated with the page to register the script block. The Page.property ClientScript returns this ClientScriptManager object. You have two overloaded methods to choose from for registering scripts; here's the one I chose for this example:

public void RegisterClientScriptBlock( Type type, string key, string script, bool addScriptTags)

Table 2 shows the meaning of the parameters to this call.

Table 2. Parameter description for RegisterClientScriptBlock method.




RegisterStartUpScript and RegisterClientScriptBlock can now take the same key name without problems because of this parameter.

This.GetType() is used in the sample


Unique value for the script block.

"CallServer" is the key value used.


The actual script block to emit.

In this sample the StringBuilder provides this value.


A bool value.

True—This would enclose the script within the "<Script>" tags.

The last thing you want to do during the page load event is to fetch the dropdown data for the region so that the user will be able to make an immediate selection from the list. When the page loads only the region dropdown list will be populated.

The next step is to pass the selected region. from the client to the server and return the processed values. The code blocks in the page load event have already added the onchange attributes for the dropdown list as follows:

cboRegion.Attributes.Add("onchange", "SelectRegion();"); cboCountry.Attributes.Add("onchange", "SelectCountry();");

Listing 2 shows the implementation of the SelectRegion function.

The JavaScript function in Listing 2 clears values (if any) in the country and city dropdown list controls. It then calls the CallServer (registered in Page_Load) method to pass the region and the context parameters.

The region parameter value is a combination of Country + delimiter + the selected region value, e.g. "Country?1, where the delimiter (the square) is a non-editable (unkeyable) character (String.FromCharCode(20)). The application generates the delimiter programmatically to ensure that the delimiting is foolproof.

The second parameter (context) is the identifier of the country dropdown list. (I will explain the reasoning behind the use of the delimiters and the context parameter later in this article.) If you recall, the server-side RaiseCallbackEvent implementation is still pending. Copy the code snippet in Listing 3 into the RaiseCallbackEvent event declaration, making sure to read the comment code.

The eventArgument parameter provides the handle to the region value from the client, e.g. "Country?1". You split this into a string array using the Char.ConvertFromUtf32(20) method. The first array element will be "Country" and the second "1". You then use a switch statement to make a correct data fetch, in this case for countries matching the region ID "1". As you can see, you have to append a delimiter to distinguish the event calls to fetch either countries or cities. Again you would append the values in "CountryId" + "ColumnDelimiter" + "CountryName" + "RowDelimiter" format. (You could accomplish this just as easily using XML.).

The resulting string value is a string of delimited values similar to: "1?India?2?China?3?Japan?4?Srilanka".

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date