Below is a basic example to explain what my concern is. I have a listview that gets data from SQL Server and has OnItemDataBound event which loops through each row of the data. And in C#, the event function ListView1_ItemDataBound is called for each row because when I place a breakpoint in the event function ListView1_ItemDataBound, it breaks once per row. Since the code written in C# is executed in the server(not sure if I'm correct on this), does it mean the user needs to connect to my server 10 separate times(10 connection requests) if there are 10 rows that listview retrieved from the database? If yes, doesn't this affect the server's performance? Is it good to avoid using 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...}
Here's the example which shows that multiple calls of ItemDataBound event doesn't match browser calls.
Here's the 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>
Here's code behind:
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");
}
}
}
And here's IE profiling tool which shows that only two requests have been executed, one for aspx and one for the image. Not three (two for ItemDataBound and one for the image.
I'm not 100% sure, but I think that under the hood ASP.NET calls ItemDataBound while building a ListView instance/HTML, not as you think when retrieves data from DataSource. To be sure you don't have multiple calls you can use mentioned IE tool.