1

下面是一个基本示例来解释我的担忧。我有一个从 SQL Server 获取数据的列表视图,并且有一个循环遍历数据的每一行的 OnItemDataBound 事件。在 C# 中,事件函数 ListView1_ItemDataBound 为每一行调用,因为当我在事件函数 ListView1_ItemDataBound 中放置一个断点时,它每行中断一次。由于用 C# 编写的代码是在服务器中执行的(不确定我是否正确),这是否意味着如果 listview 检索到 10 行,用户需要连接到我的服务器 10 次(10 个连接请求)从数据库?如果是,这不会影响服务器的性能吗?避免使用 OnItemDataBound 好吗?

aspx

<asp:Listview ID="ListviewContent" runat="server" DataKeyNames="UniqueNo" DataSourceID="SqlDataSource1" OnItemDataBound="ListView1_ItemDataBound">
     <ItemTemplate>
        <asp:Button id="btn_popup" text="Popup" runat="server" Visible="false" /> ..................

C#

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e){...Find the button control and on certain condition make it visible or whatever you need...}
4

1 回答 1

1

下面的示例显示 ItemDataBound 事件的多次调用与浏览器调用不匹配。

这是aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebApplication2.Test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ListView ID="lvTest" runat="server" OnItemDataBound="lvTest_ItemDataBound"  OnDataBinding="lvTest_DataBinding" OnDataBound="lvTest_DataBound">
                <ItemTemplate>
                    <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                </ItemTemplate>
            </asp:ListView>
            <img src="http://blaugh.lockergnome.com/cartoons/070126_web_developer.gif" />
        </div>
    </form>
</body>
</html>

这是后面的代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class Test : System.Web.UI.Page
    {
        DataTable dt;

        protected void Page_Load(object sender, EventArgs e)
        {
            Debug.WriteLine("Page_Load");
            dt = new DataTable();
            dt.Clear();
            dt.Columns.Add("Name");

            DataRow mark = dt.NewRow();
            mark["Name"] = "Mark";
            dt.Rows.Add(mark);

            DataRow bill = dt.NewRow();
            bill["Name"] = "Bill";
            dt.Rows.Add(bill);

            lvTest.DataSource = dt;
            lvTest.DataBind();
        }

        protected void lvTest_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            Debug.WriteLine("ItemDataBound");
        }

        protected void lvTest_DataBinding(object sender, EventArgs e)
        {
            Debug.WriteLine("DataBinding");
        }

        protected void lvTest_DataBound(object sender, EventArgs e)
        {
            Debug.WriteLine("DataBound");
        }
    }
}

这是 IE 分析工具,它显示只执行了两个请求,一个用于 aspx,一个用于图像。不是三个(两个用于 ItemDataBound,一个用于图像。

在此处输入图像描述

我不是 100% 确定,但我认为 ASP.NET 在构建 ListView 实例/HTML 时调用 ItemDataBound,而不是从 DataSource 检索数据时所想的那样。为确保您没有多次通话,您可以使用提到的 IE 工具。

于 2015-01-16T22:20:08.963 回答