我应该如何将数据源中的字段绑定到 ListView 控件?

发布于 2024-10-31 23:50:51 字数 1121 浏览 2 评论 0原文

我有一个 ListView 控件,用于使用 ASP ObjectDataSource 控件进行填充。这很好用。

但是,我想实现一个过滤器,显示 ListView 中以字母 A、B、C 等开头的项目。为此,我删除了 ObjectDataSource 控件,并将其替换为 Page_Load 事件中的一些代码,以便我更好地进行控制在我作为数据源传递的内容上,类似于:

System.Diagnostics.Debug.Print("{0:HH:mm:ss} : GET DATA", DateTime.Now);
List<MyItem> items = GetItems("A"); // Gets a list of items with a description that 
                                    // begins with A
MyListView.Datasource = items;
System.Diagnostics.Debug.Print("{0:HH:mm:ss} : BIND DATA", DateTime.Now);
MyListView.DataBind();
System.Diagnostics.Debug.Print("{0:HH:mm:ss} : DONE", DateTime.Now);

输出(时间代表实际结果):

16:00:00 : GET DATA
16:00:00 : BIND DATA
16:00:20 : DONE

由于这样做,在浏览器中加载页面需要大约 20 秒,而不是我使用时的大约 1 秒对象数据源。

要将数据加载到 ListView 行中,我使用标准 <%# Eval("Description") %> 方法。在SO和google上进行一些搜索后,有些人 似乎在说 Eval 效率低下。

为什么在 Page_Load 事件中手动绑定会减慢一切速度?是因为Eval效率低下吗?我怎样才能加快速度,正确的方法是什么?

I have a ListView control which I used to populate using an ASP ObjectDataSource control. This worked fine.

However, I wanted to implement a filter that showed items in the ListView that began with the letter A, B, C, etc. To do this, I removed the ObjectDataSource control and replaced it with some code in the Page_Load event allowing me greater control over what I was passing in as the data source, similar to this:

System.Diagnostics.Debug.Print("{0:HH:mm:ss} : GET DATA", DateTime.Now);
List<MyItem> items = GetItems("A"); // Gets a list of items with a description that 
                                    // begins with A
MyListView.Datasource = items;
System.Diagnostics.Debug.Print("{0:HH:mm:ss} : BIND DATA", DateTime.Now);
MyListView.DataBind();
System.Diagnostics.Debug.Print("{0:HH:mm:ss} : DONE", DateTime.Now);

Output (times are representative of actual results):

16:00:00 : GET DATA
16:00:00 : BIND DATA
16:00:20 : DONE

Since doing this, it takes about 20 seconds to load the page in my browser, instead of around 1 second when I used the ObjectDataSource.

To load the data into my ListView rows, I use the standard <%# Eval("Description") %> method. After some searching on SO and google, some people seem to say that Eval is inefficient.

Why does manual binding in the Page_Load event slow everything down? Is it because Eval is inefficient? How can I speed it up, and what is the correct way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小巷里的女流氓 2024-11-07 23:50:51

在我看来,问题不太可能是 Eval 语句或您在页面加载中进行数据绑定的事实,除非您返回一个非常非常大的列表。评估可能会慢一些,但不会像您看到的那样慢。可能还有另一个原因。

我会仔细检查 GetItems() 函数。更有可能的是,选择代码的效率在某种程度上低于其应有的水平。

其他需要检查的事情...

检查 Eval 正在调用的属性。除了返回字符串之外,他们还会做更多的事情吗? Eval 将运行这些属性/方法中的任何代码,因此请确保它们尽可能快。

It seems highly unlikely to me that the problem is the Eval statement or the the fact that you're databinding in the page load unless your returning a very very large list. Eval may be slower but not by the amount you are seeing. There is probably another cause.

I would double check the GetItems() function. It's more likely that the selection code is somehow less efficient than it could be.

Additional things to check...

Check the properties Eval is calling. Does they do something more that just return a string? Eval will run whatever code is within those properties/methods so make sure they are as fast as possible.

彩虹直至黑白 2024-11-07 23:50:51

您的数据库中有多少条记录?您是否启用了寻呼功能?如果是这样,问题可能是 ObjectDataSource 使用更有效的方法来仅检索它打算显示的对象数量,而对 GetItems() 的调用则返回所有内容,即使它没有显示。鉴于返回时间的巨大差异,这是我对正在发生的事情的猜测。

如果发生这种情况,您可以通过限制返回的记录数量来加快速度。这将取决于您对 GetItems() 的实现。您想要编写类似 GetItemsPaged(int firstRecord, int pageLength) 的代码,该代码仅返回有限数量的数据。

How many records are in your database? Do you have paging enabled? If so, the problem might be that the ObjectDataSource is using a more efficent method to retrieve only the number of objects that it intends on displaying, whereas your call to GetItems() is returning everything, even if it isn't being displayed. Given the huge disparity in the time to return, that is my guess as to what is happening.

If that is what is happening, you speed it up by limiting the number of records you are returning. This is going to depend on your implementation of GetItems(). You'd want to write something like GetItemsPaged(int firstRecord, int pageLength) that returns only a limited amount of data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文