views:

32

answers:

2

I'm trying to make a decision about how to display my data. What I have now is a list of products displayed in a repeater. But for code-maintenance I've put my product items in a seperate usercontrol and loading them in a loop with db results using LoadControl.

The product control itself is very simple, just a few public properties like title, url, rating but I'm not sure if this will affect my performance. I did some reading here and on forums and some people say it's not the best practice especially if you have more then 20 or 30 of these controls.

So, is it really a performance hit using this method or does it stay ok with around 10.000 hits a day. Any other suggestions are welcome as well.

A: 

I think that making a separate user control for each product probably isn't the best way to go.

However, you're already partially where you need to be for my suggestion.

Why don't you make a class for your Product, with all of the properties you've already defined on your user control.

When you retrieve the products you are displaying, retrieve a collection of Products (loop through the records and create the collection)(List for example), and use the collection as the DataSource for your Repeater.

TheGeekYouNeed
hello, thanks for your reply. The thing is I'm going to display the products in more then 1 location, also in Search results and for example in the section 'Similar products'. And what I've done is created just 1 Usercontrol to make it more maintainable because the html will be the same on all sections. It work's ok but I think the main question is if it's a performance hit to user loadcontrol in a loop. the products itselve are already contained in classes from my DAL. Thank you
Mark
Ok, I had understood from what you wrote that each ItemRow was a usercontrol. Cache your product collection, that will save you a lot of processing time.
TheGeekYouNeed
A: 

I did some testing because I was interested in this as well, and it seems that there is like a pretty significant performance hit on LoadControl compared to instantiating a normal class.

I tested creating 100,000 instances of a simple class with one property vs 100,000 instances of loadcontrol loading a new blank control. And it was 16ms for the classes vs 1950ms for the load control. There seems to be a lot of overhead. I did notice that if I added more constituent controls to the user control the load time went up. (this is just the load I did not actually add the controls to the page or render them)

Is it enough of a hit to be noticeable to a user? Probably not unless you are loading a large amount of instances with loadcontrol.

Chris Mullins
Hi Chris, thank you very much for testing this out. There will be a maximum of 30 controls loaded each time so I will take my chances with it. Im gonna try to load the control one time and re-use it in subsequent iterations but I don't think it's possible. I'm gonna do something testing also and let you know later today how it went. Thanks.
Mark
Hi Chris, I did a little bit of testing, the fastest way to use the LoadControl method is creating it outside the loop and set its properties inside the loop, but only the last instance get added to the placeholder, not sure on if this method is possible at all though. then I made a product collection by hand (only 500 since I wanted to render them also) and thought what if I put the control inside a repeater and databind the repeater. The funny thing was that execution times were nearly identical then using the Loadcontrol inside a loop so i guess it used that behind the screens too.
Mark
Sorry, i should write in my question field I guess but anyways I'm sticking to use the loadcontrol inside a loop since it's still rather fast and easy maintainable. Cheers!
Mark
It will only let you add an instance of user control to the page once. Even if you change the ID before you add each time. You are probably right about the loadcontrol method is used no matter what even if you put it in the repeater.
Chris Mullins