JavaScript Editor jscript editor     Web designer 

Main Page

Demonstrates an ASP.NET Web page that implements a client callback. For more information, see Implementing Client Callbacks Without Postbacks in ASP.NET Web Pages.


The following code example is in two parts. The first part of the example shows an ASP.NET Web page (the .aspx page). The second part shows the corresponding code-behind file (the .aspx.cs file).

C#В CopyCode imageCopy Code
<%@ Page Language="C#" AutoEventWireup="true" 
  CodeFile="ClientCallback.aspx.cs" Inherits="ClientCallback" %>

  1.1//EN" "">

<html xmlns="" >
<head runat="server">
  <script type="text/javascript">
    function LookUpStock()
        var lb = document.forms[0].ListBox1;
        var product = lb.options[lb.selectedIndex].text 
        CallServer(product, "");
    function ReceiveServerData(rValue)
        Results.innerText = rValue;
  <form id="form1" runat="server">
      <asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>
      <br />
      <br />
      <button onclick="LookUpStock()">Look Up Stock</button>
      <br />
      <br />
      Items in stock: <span ID="Results"></span>
      <br />

// ClientCallback.aspx.cs code-behind page
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ClientCallback : System.Web.UI.Page,
    protected System.Collections.Specialized.ListDictionary catalog;
    protected void Page_Load(object sender, EventArgs e)
        String cbReference =
            "arg", "ReceiveServerData", "context");
        String callbackScript;
        callbackScript = "function CallServer(arg, context)" +
            "{ " + cbReference + "} ;";
            "CallServer", callbackScript, true);

        catalog = new System.Collections.Specialized.ListDictionary();
        catalog.Add("monitor", 12);
        catalog.Add("laptop", 10);
        catalog.Add("keyboard", 23);
        catalog.Add("mouse", 17);

        ListBox1.DataSource = catalog;
        ListBox1.DataTextField = "key";

    public String RaiseCallbackEvent(String eventArgument)
        String returnValue;
        if (catalog[eventArgument] == null)
            returnValue = "-1";
            returnValue = catalog[eventArgument].ToString();
        return returnValue;

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, which returns the available stock for the product passed to it. Note that the arguments sent between the client script and the server code can only be strings.

Security noteSecurity 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.

See Also

JavaScript Editor jscript editor     Web designer