为什么ASP ListBox需要这么长时间才能在AJAX更新面板中呈现?

时间:2009-12-03 20:55:43

标签: .net asp.net vb.net asp.net-ajax listbox

在AJAX更新面板中更新ASP ListBox的源代码时遇到问题。当我将ListBox的源设置为大型数据集时,我认为由于项目数而需要花费少量时间来渲染。但是,当DataSource在运行时切换到较小的项目集时,清除它所需的时间也一样长。如果你从一小部分物品转到一小部分物品,这是闪电般快速的。也许我做错了什么。我正在使用Visual Studio 2008项目模板AJAX 1.0-Enabled ASP.NET 2.0 Web Application。我是从Microsoft下载的。

这是我的代码(下面的完整源码):

Default.aspx的

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestingAJAXComboLoadTimes._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:ListBox ID="myListBox" runat="server" Rows="12" Width="100%" DataTextField="Display"
                DataValueField="Value" AutoPostBack="True" />
            <asp:Button ID="myButton" runat="server" Text="Change List" />
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

Default.aspx.vb

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 - " & x, x))
        Next
        For x As Integer = 0 To 50
            _ListB.Add(New MyModel("testing B - " & 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

MyModel类(包含在Default.aspx.vb中)

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

我没有修改web.config文件,但这里是为了完整性:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
            <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
                <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
                    <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/>
                    <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
                </sectionGroup>
            </sectionGroup>
        </sectionGroup>
    </configSections>
    <system.web>
        <pages>
            <controls>
                <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            </controls>
        </pages>
        <!--
          Set compilation debug="true" to insert debugging
          symbols into the compiled page. Because this
          affects performance, set this value to true only
          during development.
    -->
        <compilation debug="true">
            <assemblies>
                <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            </assemblies>
        </compilation>
        <httpHandlers>
            <remove verb="*" path="*.asmx"/>
            <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            <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"/>
            <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"/>
        </httpHandlers>
        <httpModules>
            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </httpModules>
    </system.web>
    <system.web.extensions>
        <scripting>
            <webServices>
                <!-- Uncomment this line to customize maxJsonLength and add a custom converter -->
                <!--
      <jsonSerialization maxJsonLength="500">
        <converters>
          <add name="ConvertMe" type="Acme.SubAcme.ConvertMeTypeConverter"/>
        </converters>
      </jsonSerialization>
      -->
                <!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. -->
                <!--
        <authenticationService enabled="true" requireSSL = "true|false"/>
      -->
                <!-- 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. -->
                <!--
      <profileService enabled="true"
                      readAccessProperties="propertyname1,propertyname2"
                      writeAccessProperties="propertyname1,propertyname2" />
      -->
            </webServices>
            <!--
      <scriptResourceHandler enableCompression="true" enableCaching="true" />
      -->
        </scripting>
    </system.web.extensions>
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <modules>
            <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </modules>
        <handlers>
            <remove name="WebServiceHandlerFactory-Integrated"/>
            <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"/>
            <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"/>
            <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"/>
        </handlers>
    </system.webServer>
</configuration>

这是我的Google Code项目和我的Source Code。你能告诉我为什么清理这些物品并设置一个新的物品清单需要这么长时间吗?

谢谢,

斯科特

2 个答案:

答案 0 :(得分:1)

你确实知道UpdatePanel做了一个完整的回发不是吗?就个人而言,我不会因此而使用它。您可以使用jQuery和PageMethods对数据源和控件执行轻量级ajax操作。

This is a nice walkthrough ...

修改:article on full postback performance issues

When a PostBack occurs in an UpdatePanel the page’s entire ViewState is passed to the server, updated, and then the updates are passed back down to the ASP.NET page. So the size of your page’s ViewState has a HUGE effect on the performance of your UpdatePanel postbacks.

答案 1 :(得分:1)

最可能的罪魁祸首是viewstate。

启用viewstate后,ASP.NET会将所有ListBox数据发送到客户端(以及无数的控件设置)。

客户端UpdatePanel操作然后将整个视图状态发送回服务器。如果您的数据集很大,那可能会显着降低速度,这可能是为什么从大型数据集转移到小型数据集需要很长时间。

您可以通过禁用列表框的viewstate来加快速度,但这意味着您必须手动在页面中执行更多操作 - 例如,您必须在每个帖子上重新绑定列表框。 / p>

文章ASP.NET Simplifies State Management in Web Applications可以很好地概述正在发生的事情和利害攸关的事项(请参阅第3页上标题为“谨慎使用Viewstate”的部分。)