C# Asp.net OnItemDataBound函数是否会影响性能?

C# Asp.net OnItemDataBound函数是否会影响性能?,c#,asp.net,performance,data-connections,C#,Asp.net,Performance,Data Connections,下面是一个基本的例子来解释我关心的是什么。我有一个listview,它从SQL Server获取数据,并有一个OnItemDataBound事件,该事件循环遍历每一行数据。在C#中,每行调用事件函数ListView1_ItemDataBound,因为当我在事件函数ListView1_ItemDataBound中放置断点时,它每行中断一次。由于用C#编写的代码是在服务器上执行的(不确定我的回答是否正确),如果listview从数据库中检索到10行,是否意味着用户需要分别连接到我的服务器10次(10

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

aspx


下面的示例显示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>
这是IE分析工具,它显示只有两个请求被执行,一个用于aspx,一个用于映像。不是三个(两个用于ItemDataBound,一个用于图像)


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

感谢您的帮助。这让您更清楚地理解。很抱歉,回复太晚了。
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e){...Find the button control and on certain condition make it visible or whatever you need...}
<%@ 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");
        }
    }
}