title: Why does ASP ListBox take so long to render in AJAX update panel?
body: <p>I have a problem when updating the source of an ASP ListBox in an AJAX update panel.  When I set the source of the ListBox to a large dataset, I would assume it would take a small amount of time to render due to the number of items.  However, when the DataSource is switched at run-time to a smaller set of items, it takes just as long to clear it.  If you go from a small set of items to a small set of items this is lightning fast.  Maybe I'm doing something wrong.  I'm using the Visual Studio 2008 item template for an <code>AJAX 1.0-Enabled ASP.NET 2.0 Web Application</code>.  I downloaded that from <a href="http://download.microsoft.com/download/2/4/1/24178c52-ae77-4547-b656-5ad776b1b08f/ASPNETAJAXVS2008.msi" rel="nofollow noreferrer">Microsoft</a>.  </p>
  
  <p>Here is my code (Full source zip below): </p>
  
  <p>Default.aspx</p>
  
  <pre><code>&lt;%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestingAJAXComboLoadTimes._Default" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;Untitled Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;asp:ScriptManager ID="ScriptManager1" runat="server" /&gt;
      &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt;
          &lt;ContentTemplate&gt;
              &lt;asp:ListBox ID="myListBox" runat="server" Rows="12" Width="100%" DataTextField="Display"
                  DataValueField="Value" AutoPostBack="True" /&gt;
              &lt;asp:Button ID="myButton" runat="server" Text="Change List" /&gt;
          &lt;/ContentTemplate&gt;
      &lt;/asp:UpdatePanel&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Default.aspx.vb</p>
  
  <pre><code>Partial Public Class _Default
      Inherits System.Web.UI.Page
  
      Private _ListA As IList(Of MyModel) = New List(Of MyModel)
      Private _ListB As IList(Of MyModel) = New List(Of MyModel)
  
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
          LoadData()
          If Not Page.IsPostBack Then
              myListBox.DataSource = _ListA
              myListBox.DataBind()
          End If
      End Sub
  
      Private Sub LoadData()
          For x As Integer = 0 To 5000
              _ListA.Add(New MyModel("testing A - " &amp; x, x))
          Next
          For x As Integer = 0 To 50
              _ListB.Add(New MyModel("testing B - " &amp; x, x))
          Next
      End Sub
  
      Private Sub SwitchDataSource()
          If IsALoaded Then
              myListBox.DataSource = _ListB
          Else
              myListBox.DataSource = _ListA
          End If
          IsALoaded = Not IsALoaded
          myListBox.DataBind()
      End Sub
  
      Private Sub myButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles myButton.Click
          SwitchDataSource()
      End Sub
  
      Public Property IsALoaded() As Boolean
          Get
              Return CBool(ViewState("IsALoaded"))
          End Get
          Set(ByVal value As Boolean)
              ViewState("IsALoaded") = value
          End Set
      End Property
  End Class
  </code></pre>
  
  <p>MyModel Class (contained within Default.aspx.vb)    </p>
  
  <pre><code>Public Class MyModel
      Private _Display As String
      Private _Value As Integer
      Public Sub New(ByVal display As String, ByVal value As Integer)
          _Display = display
          _Value = value
      End Sub
      Public ReadOnly Property Display() As String
          Get
              Return _Display
          End Get
      End Property
      Public ReadOnly Property Value() As Integer
          Get
              Return _Value
          End Get
      End Property
  End Class
  </code></pre>
  
  <p>I didn't modify the web.config file, but here it is for completeness: </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
      &lt;configSections&gt;
          &lt;sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"&gt;
              &lt;sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"&gt;
                  &lt;section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/&gt;
                  &lt;sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"&gt;
                      &lt;section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/&gt;
                      &lt;section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/&gt;
                      &lt;section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/&gt;
                  &lt;/sectionGroup&gt;
              &lt;/sectionGroup&gt;
          &lt;/sectionGroup&gt;
      &lt;/configSections&gt;
      &lt;system.web&gt;
          &lt;pages&gt;
              &lt;controls&gt;
                  &lt;add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/&gt;
              &lt;/controls&gt;
          &lt;/pages&gt;
          &lt;!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
      --&gt;
          &lt;compilation debug="true"&gt;
              &lt;assemblies&gt;
                  &lt;add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/&gt;
              &lt;/assemblies&gt;
          &lt;/compilation&gt;
          &lt;httpHandlers&gt;
              &lt;remove verb="*" path="*.asmx"/&gt;
              &lt;add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/&gt;
              &lt;add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/&gt;
              &lt;add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/&gt;
          &lt;/httpHandlers&gt;
          &lt;httpModules&gt;
              &lt;add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/&gt;
          &lt;/httpModules&gt;
      &lt;/system.web&gt;
      &lt;system.web.extensions&gt;
          &lt;scripting&gt;
              &lt;webServices&gt;
                  &lt;!-- Uncomment this line to customize maxJsonLength and add a custom converter --&gt;
                  &lt;!--
        &lt;jsonSerialization maxJsonLength="500"&gt;
          &lt;converters&gt;
            &lt;add name="ConvertMe" type="Acme.SubAcme.ConvertMeTypeConverter"/&gt;
          &lt;/converters&gt;
        &lt;/jsonSerialization&gt;
        --&gt;
                  &lt;!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. --&gt;
                  &lt;!--
          &lt;authenticationService enabled="true" requireSSL = "true|false"/&gt;
        --&gt;
                  &lt;!-- Uncomment these lines to enable the profile service. To allow profile properties to be retrieved
             and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and
             writeAccessProperties attributes. --&gt;
                  &lt;!--
        &lt;profileService enabled="true"
                        readAccessProperties="propertyname1,propertyname2"
                        writeAccessProperties="propertyname1,propertyname2" /&gt;
        --&gt;
              &lt;/webServices&gt;
              &lt;!--
        &lt;scriptResourceHandler enableCompression="true" enableCaching="true" /&gt;
        --&gt;
          &lt;/scripting&gt;
      &lt;/system.web.extensions&gt;
      &lt;system.webServer&gt;
          &lt;validation validateIntegratedModeConfiguration="false"/&gt;
          &lt;modules&gt;
              &lt;add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/&gt;
          &lt;/modules&gt;
          &lt;handlers&gt;
              &lt;remove name="WebServiceHandlerFactory-Integrated"/&gt;
              &lt;add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/&gt;
              &lt;add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/&gt;
              &lt;add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/&gt;
          &lt;/handlers&gt;
      &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Here is my <a href="https://code.google.com/p/ajax-listbox-loadtime/" rel="nofollow noreferrer">Google Code</a> project, and my <a href="http://ajax-listbox-loadtime.googlecode.com/files/TestingAJAXListBoxLoadTimes.zip" rel="nofollow noreferrer">Source Code</a>.  Can you tell me why it takes so long to clear the items and set a new list of items? </p>
  
  <p>Thank you, </p>
  
  <p>Scott</p>