Microsoft KB Archive/910450

= Implementing asynchronous callbacks in DropDownList controls, in ListBox controls, and in Label controls in ASP.NET 2.0 =

Article ID: 910450

Article Last Modified on 12/14/2006

-

APPLIES TO


 * Microsoft ASP.NET 2.0

-



Implementing asynchronous callbacks in DropDownList controls, in ListBox controls, and in Label controls in ASP.NET 2.0
''To customize this column to your needs, we want to invite you to submit your ideas about topics that interest you and issues that you want to see addressed in future Knowledge Base articles and Support Voice columns. You can submit your ideas and feedback using the Ask For It form. There's also a link to the form at the bottom of this column.''



Welcome to the Microsoft ASP.NET Support Voice column! I am Nilay B. Shah from the ASP.NET developer support team. I have been working with the ASP.NET support group for more than two years. Currently, I am focusing on the asynchronous callback (Client callbacks) functionality that has been added to ASP.NET 2.0. In this column, I am going to discuss a few sample applications that can be integrated into any of the ASP.NET 2.0 applications to enhance user experience. I would like to thank Jerry Orman from the ASP.NET support team for his continuous inspiration and help.



Introduction
The GridView control and the TreeView controls in ASP.NET 2.0 provide a rich, interactive user experience without full page postbacks. However, I have worked with customers who want help in performing asynchronous callbacks in other Web server controls. This article describes how the same functionality can be implemented in ListBox controls and in DropDownList controls and includes a way to increment the value of a Label control asynchronously.

The following topics are covered in this article:
 * How to update the ListBox Web server control value by using asynchronous callbacks depending on the TextBox control value
 * How to update the DropDownList Web server control value by using asynchronous callbacks depending on another DropDownList control value
 * How to increment a value in a Label control that is based on a button click in asynchronous callbacks



How to update the ListBox Web server control value by using asynchronous callbacks depending on the TextBox control value
To update a ListBox Web server control value by using asynchronous callbacks, follow these steps:
 * 1) Register callback handlers by using the Page.ClientScript.GetCallbackEventReference method.
 * 2) Add an appropriate attribute to the TextBox Web server control to instantiate the asynchronous callback.
 * 3) Retrieve the ListBox control's data based on the TextBox control value that is passed as an argument-string from the client-function, and then return the data to the client in the form of a string.
 * 4) Write a script to parse the Document Object Model (DOM) in the browser.
 * 5) Update the contents.

The following example uses a TextBox Web server control and a ListBox Web server control to display the new functionality. This sample uses the Northwind sample database's Employees table. When you start entering text for the LastName field in the TextBox control, asynchronous callbacks occur. The value of the TextBox control is passed back to the server. The employee's first name is fetched from the Employees database table depending on the text that is passed, and the data is returned to the client in a formatted string. There is a client-side JavaScript function that processes the string value and appends it to the ListBox control.

In the following code sample, the asynchronous ClientCallback script is registered to the TextBox control's OnKeyUp event in the Page_Load event of the page. txtLastName.Attributes[&quot;OnKeyUp&quot;] = TextBoxEventScript; This lets the ListBox control values be retrieved based on the text that is entered in the TextBox control.

In the RaiseCallbackEvent method, the field values that match the LastName value that is passed to the server are retrieved from the database. The results of the database call are built into a pipe-delimited list in the following format:

FirstName | EmployeeID || FirstName | EmployeeID

The resulting string is sent back to the client through the GetCallbackResult function. The ReceiveServerData JavaScript function retrieves values from the delimited string and appends the values to the ListBox control. lstFirstName.appendChild(option); ASPX markup <%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;UpdateListboxBasedOnTextbox.aspx.cs&quot; Inherits=&quot;UpdateListboxBasedOnAnotherListbox&quot; %>

<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>

  Untitled Page

<!—JavaScript function that will run when the OnKeyUp event of the TextBox control runs on the client. -->    function ReceiveServerData(arg,context) {       var lstFirstName = document.forms[0].elements['lstFirstName']; lstFirstName.innerHTML= &quot;&quot;; var rows = arg.split('||'); for (var i = 0; i < rows.length - 1; ++i) {        var fields = rows[i].split('|'); var firstname = fields[0]; var employeeid = fields[1]; var option = document.createElement(&quot;option&quot;); option.value = employeeid; option.innerHTML = firstname; lstFirstName.appendChild(option); }   }      

 

 

  <asp:SqlDataSource ID=&quot;SqlDataSource1&quot; runat=&quot;server&quot; ConnectionString=&quot;<%$ ConnectionStrings:NorthwindConnectionString %>&quot; SelectCommand=&quot;SELECT [firstname], [lastname] FROM [Employees]&quot;> </asp:SqlDataSource>

<asp:Label ID=&quot;lblText&quot; runat=&quot;server&quot; Font-Names=&quot;Arial&quot; Font-Size=&quot;Small&quot; Font-Underline=&quot;True&quot; Style=&quot;z-index: 106; left: 32px; position: absolute; top: 56px&quot; Text=&quot;Enter Employee's LastName in textbox and FirstName in Listbox Will Be Updated Asynchronously&quot; Width=&quot;600px&quot;> </asp:Label> <asp:Label ID=&quot;lblTitle&quot; runat=&quot;server&quot; Font-Bold=&quot;True&quot; Font-Names=&quot;Verdana&quot; Style=&quot;z-index: 105;left: 8px; position: absolute; top: 16px&quot; Text=&quot;Updating ListBox Control Asynchronously&quot; Width=&quot;488px&quot;> </asp:Label> <span id=&quot;message&quot;> Code behind 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; using System.Text; using System.Data.SqlClient; using System.Web.Configuration;

public partial class UpdateListboxBasedOnAnotherListbox : System.Web.UI.Page,ICallbackEventHandler {

//This object is used to build the results of the callback function StringBuilder arg;

protected void Page_Load(object sender, EventArgs e)   { //Do not process Page_Load if it is a clientscript callback. if (IsCallback) return;

if (!Page.IsPostBack) {           //Generate an HTML script that can instantiate an asynchronous callback onKeyUp event of             //txtLastName string TextBoxEventScript = Page.ClientScript.GetCallbackEventReference(this, &quot;document.all['txtLastName'].value&quot;, &quot;ReceiveServerData&quot;, &quot;null&quot;); txtLastName.Attributes[&quot;OnKeyUp&quot;] = TextBoxEventScript; }   }

// ********************************************************************   // Implement the RaiseCallbackEvent function of the callback interface // ********************************************************************

public void RaiseCallbackEvent(String eventArgument) {

//Connect to the database and look up the employee based on the value passed to the server. //In this case, the textbox value is passed in the eventArgument.

SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings[&quot;NorthwindConnectionString&quot;].ConnectionString);

try {           SqlCommand cmd = new SqlCommand; cmd.CommandText = &quot;Select firstname,employeeid from Employees where LastName like @LastName&quot;; cmd.Parameters.Add(new SqlParameter(&quot;@LastName&quot;, SqlDbType.NVarChar, 50)); cmd.Parameters[&quot;@LastName&quot;].Value = eventArgument.ToString + &quot;%&quot; ; cmd.Connection = con;

con.Open; arg = new StringBuilder; SqlDataReader reader = cmd.ExecuteReader;

while (reader.Read) {               arg.Append(reader[&quot;firstname&quot;]); arg.Append(&quot;|&quot;); arg.Append(reader[&quot;employeeid&quot;]); arg.Append(&quot;||&quot;); }           reader.Close; }       catch (Exception ex) {           Response.Write(ex.Message); }       finally {           con.Close;

}   }

// ********************************************************************   // Implement the GetCallbackResult function of the callback interface // ********************************************************************   public string GetCallbackResult {       return arg.ToString; } }

How to update the DropDownList Web server control value by using asynchronous callbacks depending on another DropDownList control value
To update the DropDownList Web server control value by using asynchronous callbacks depending on another DropDownList control value, follow these steps:
 * 1) Register callback handlers by using the Page.ClientScript.GetCallbackEventReference method.
 * 2) Add appropriate attributes to the first DropDownList Web server control to instantiate asynchronous callbacks.
 * 3) Retrieve the second DropDownList control's data based on the DropDownList control value that is passed from the client-side JavaScript function.
 * 4) Return the values to the client in the form of a delimited string.
 * 5) Write a script to parse the DOM in the browser, and then update the contents.

The following code sample uses two DropDownList controls, drpEmployeeId and drpEmployeeName. This sample uses the sample Northwind database's Employees table. The drpEmployeeId control is data-bound to the EmployeeId column. When you select a value from the drpEmployeeId control, an asynchronous callback occurs. The value of the drpEmployeeId control is passed back to the server. The employee's first name is retrieved from the Employees table based on the EmployeeId data that was passed from the client. The value is returned to the client in a delimited string. The client-side JavaScript function processes the string value, and then appends the values to the drpEmployeeNameDropDownList control.

ASPX markup <%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;UpdateDropDownboxBasedOnAnotherDropDownBox.aspx.cs&quot; Inherits=&quot;UpdateDropDownboxBasedOnAnotherListbox&quot; %>

<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>

<html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; > <head runat=&quot;server&quot;> Asynchronous Callback - DropDownList <script language=&quot;javascript&quot;> function ReceiveServerData(arg,context) {       var drpEmployeeName = document.forms[0].elements['drpEmployeeName']; drpEmployeeName.innerHTML= &quot;&quot;; var rows = arg.split('||'); for (var i = 0; i < rows.length - 1; ++i) {        var fields = rows[i].split('|'); var firstname = fields[0]; var employeeid = fields[1]; var option = document.createElement(&quot;option&quot;); option.value = employeeid; option.innerHTML = firstname; drpEmployeeName.appendChild(option); document.getElementById('lblMessage').innerText = &quot;You selected employeeid: &quot; + employeeid  + &quot; for which FirstName is &quot; + firstname ; }       }

<form id=&quot;form1&quot; runat=&quot;server&quot;> <asp:DropDownList ID=&quot;drpEmployeeId&quot; runat=&quot;server&quot; DataSourceID=&quot;SqlDataSource1&quot; DataTextField=&quot;employeeid&quot; DataValueField=&quot;employeeid&quot; Style=&quot;z-index: 100; left: 144px; position: absolute; top: 112px&quot;> </asp:DropDownList> <asp:DropDownList ID=&quot;drpEmployeeName&quot; runat=&quot;server&quot; Style=&quot;z-index: 101; left: 144px; position: absolute; top: 136px&quot;> </asp:DropDownList> <asp:Label ID=&quot;lblEmployeeId&quot; runat=&quot;server&quot; Style=&quot;z-index: 102; left: 48px; position: absolute; top: 112px&quot; Text=&quot;EmployeeID:&quot; Width=&quot;88px&quot;></asp:Label> <asp:Label ID=&quot;lblFirstName&quot; runat=&quot;server&quot; Style=&quot;z-index: 103; left: 48px; position: absolute; top: 136px&quot; Text=&quot;First Name&quot; Width=&quot;88px&quot;></asp:Label> <asp:Label ID=&quot;lblText&quot; runat=&quot;server&quot; Font-Names=&quot;Arial&quot; Font-Size=&quot;Small&quot; Style=&quot;z-index: 104; left: 48px; position: absolute; top: 80px&quot; Text=&quot;Select EmployeeId from first DropDown and Second DropDown Will Be Updated Asynchronously&quot; Width=&quot;600px&quot; Font-Underline=&quot;True&quot;></asp:Label> <asp:Label ID=&quot;lblTitle&quot; runat=&quot;server&quot; Font-Bold=&quot;True&quot; Font-Names=&quot;Verdana&quot; Style=&quot;z-index: 105; left: 24px; position: absolute; top: 24px&quot; Text=&quot;Updating DropDownListBox Control Asynchronously&quot; Width=&quot;488px&quot;></asp:Label> <asp:Label ID=&quot;lblMessage&quot; runat=&quot;server&quot; Style=&quot;z-index: 107; left: 48px; position: absolute; top: 192px&quot; Width=&quot;592px&quot; Font-Names=&quot;Arial&quot; Font-Size=&quot;Small&quot; ForeColor=&quot;MediumBlue&quot;></asp:Label> <asp:SqlDataSource ID=&quot;SqlDataSource1&quot; runat=&quot;server&quot; ConnectionString=&quot;<%$ ConnectionStrings:NorthwindConnectionString %>&quot; SelectCommand=&quot;SELECT [employeeid], [firstname] FROM [Employees]&quot;> </asp:SqlDataSource> Code behind 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; using System.Web.Configuration; using System.Text; using System.Data.SqlClient;

public partial class UpdateDropDownboxBasedOnAnotherListbox : System.Web.UI.Page, ICallbackEventHandler {   StringBuilder arg; protected void Page_Load(object sender, EventArgs e)   { //Do not process Page_Load if it is a clientscript callback. if (IsCallback) return;

if (!Page.IsPostBack) {           //Generate an HTML script that can instantiate an asynchronous callback on the click of             //the first DropDownListBox, and add it to the &quot;onClick&quot; attribute of the same. string FirstDropDownCallbackScript = Page.ClientScript.GetCallbackEventReference(this, &quot;document.all['drpEmployeeId'].value&quot;, &quot;ReceiveServerData&quot;, &quot;null&quot;); drpEmployeeId.Attributes[&quot;onClick&quot;] = FirstDropDownCallbackScript; }   }

// *******************************************************   // Implement the callback interface public void RaiseCallbackEvent(String eventArgument) {       SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings[&quot;NorthwindConnectionString&quot;].ConnectionString);

try {           SqlCommand cmd = new SqlCommand; cmd.CommandText = &quot;Select firstname,employeeid from Employees where employeeid= @EmployeeId&quot;; cmd.Parameters.Add(new SqlParameter(&quot;@EmployeeId&quot;, SqlDbType.Int, 4)); cmd.Parameters[&quot;@EmployeeId&quot;].Value = Int32.Parse(eventArgument); cmd.Connection = con;

arg = new StringBuilder; con.Open;

SqlDataReader reader = cmd.ExecuteReader;

while (reader.Read) {               arg.Append(reader[&quot;firstname&quot;]); arg.Append(&quot;|&quot;); arg.Append(reader[&quot;employeeid&quot;]); arg.Append(&quot;||&quot;); }           reader.Close; }       catch (Exception ex) {           Response.Write(ex.Message); }       finally {           con.Close; }   }

public string GetCallbackResult {       return arg.ToString ; }   // ******************************************************* }

How to increment a value in a Label control that is based on a button click in asynchronous callbacks
In the following code sample, a counter is incremented on each button click, and the value of the counter is placed in a Label control. The count value is maintained in a Session variable that is named Count to track the value across requests.

This sample uses session state to store the counter because there is no support for controlling the ViewState property in asynchronous callbacks. When you implement page properties or control properties that need to be updated during script callback operations, avoid using the ViewState property.

ASPX markup <%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;IncrementingCountInLabelControl.aspx.cs&quot; Inherits=&quot;IncrementingCountInLabelControl&quot; %>

<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>

<html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; > <head runat=&quot;server&quot;> Asynchronous Callback - Label <script language=&quot;javascript&quot;> function ReceiveServerData(arg,context) {       document.getElementById('lblCounter').innerText = arg; }   <form id=&quot;form1&quot; runat=&quot;server&quot;> <asp:Label ID=&quot;lblCount&quot; runat=&quot;server&quot; Style=&quot;z-index: 100; left: 48px; position: absolute; top: 80px&quot; Text=&quot;Count :&quot; Width=&quot;56px&quot; Font-Names=&quot;Arial&quot;></asp:Label> <asp:Label ID=&quot;lblTitle&quot; runat=&quot;server&quot; Font-Bold=&quot;True&quot; Font-Names=&quot;Verdana&quot; Style=&quot;z-index: 101; left: 8px; position: absolute; top: 16px&quot; Text=&quot;Incrementing Count on each Button-Click Asynchronously&quot; Width=&quot;552px&quot;></asp:Label> <input id=&quot;btnClickMe&quot; style=&quot;z-index: 104; left: 48px; position: absolute; top: 104px&quot; type=&quot;button&quot; onclick=&quot;CallServer(, )&quot; value=&quot;Click Me&quot; /> <asp:Label ID=&quot;lblText&quot; runat=&quot;server&quot; Font-Names=&quot;Arial&quot; Font-Size=&quot;Small&quot; Font-Underline=&quot;True&quot; Style=&quot;z-index: 102; left: 32px; position: absolute; top: 48px&quot; Text=&quot;Click Button and Observe count:&quot; Width=&quot;600px&quot;></asp:Label> <asp:Label ID=&quot;lblCounter&quot; runat=&quot;server&quot; Font-Names=&quot;Arial&quot; Font-Size=&quot;Large&quot; Style=&quot;z-index: 105; left: 104px; position: absolute; top: 80px&quot; Text=&quot;0&quot; Width=&quot;24px&quot;></asp:Label> Code behind 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; using System.Text;

public partial class IncrementingCountInLabelControl : System.Web.UI.Page,ICallbackEventHandler {

protected void Page_Load(object sender, EventArgs e)   { //Do not process Page_Load if it is a clientscript callback. if (IsCallback) return;

if (!Page.IsPostBack) {           Session[&quot;Count&quot;] = &quot;0&quot;; ClientScriptManager cm = Page.ClientScript; String cbReference = cm.GetCallbackEventReference(this, &quot;arg&quot;, &quot;ReceiveServerData&quot;, &quot;&quot;); String callbackScript = &quot;function CallServer(arg, context) {&quot; + cbReference + &quot;; }&quot;; cm.RegisterClientScriptBlock(this.GetType, &quot;CallServer&quot;, callbackScript, true); }   }

// *******************************************************   // Implement the callback interface public void RaiseCallbackEvent(String eventArgument) {         Session[&quot;Count&quot;]  = int.Parse((Session[&quot;Count&quot;].ToString)) + 1; }

public string GetCallbackResult {       return Session[&quot;Count&quot;].ToString; }   // *******************************************************

}

Conclusion
Although asynchronous callbacks are limited in terms of passing the values from the client to the server as a string and requiring you to write some Javascript, it is possible to update Web server controls other than the GridView control and the TreeView control asynchronously. A little extra effort can enhance the user experience to a great extent!

Related links
For more information, visit the following Microsoft Web sites:

http://msdn2.microsoft.com/en-us/library/ms178208(VS.80).aspx

http://msdn.microsoft.com/msdnmag/issues/04/08/CuttingEdge/

http://msdn2.microsoft.com/en-us/library/ms153103(VS.80).aspx

http://msdn2.microsoft.com/en-us/library/ms189826.aspx

For more information about DOM HTML, visit the following World Wide Web Consortium (W3C) Web site:

http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-642250288

<div class="moreinformation_section">

I hope you found this information helpful. Remember, the Support Voice columns are for you! As always, feel free to submit ideas on topics you want addressed in future columns or in the Knowledge Base using the Ask For It form.

Keywords: kbhowto kbasp KB910450

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.