The Web page emulates a database lookup to determine the number of items that are available, or in stock, for a series of products (monitors, keyboards, and so on). To simplify this code example, the database is represented by a dictionary list that contains a small set of items. For each item in the table, the key is the item name (such as monitor) and the value is the number of items that are in stock. In a production application, a database would be used instead.
When the page runs, a ListBox control is bound to the hash table so that the ListBox control displays the list of products. The page also contains a button element (not a Button Web server control), whose onclick event is bound to a client function named LookUpStock. When users click the button, the button executes the LookUpStock function, which gets the current selection from the list box and then performs the client callback by calling the CallServer function.
The code-behind page adds client-side script to the page via the RegisterClientScriptBlock method. The script that is added to the page includes a function called CallServer, which gets the name of the method that will post back to the server from the GetCallbackEventReference method.
The client callback invokes the RaiseCallbackEvent method, to determine the available stock for the product passed to it. The GetCallbackResult method returns the value. Note that the arguments sent between the client script and the server code can only be strings. To pass in or to receive multiple values, you can concatenate values in the input or return string, respectively.
Security Note: |
|---|
When you use this feature, there are potential security threats. Callback arguments are not validated and therefore should be considered unsafe. You should always check the contents of the arguments before using them. For details, see Script Exploits Overview. |