开发者

How should I bind fields from a datasource to a ListView control?

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 standar开发者_如何转开发d <%# 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜