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 f开发者_Go百科or 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.
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.
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.
When you use Page.LoadControl
within a loop, separate it into it's own function and this will tell ASP.Net
to cache the control instead of loading it from disk every time.
Example: (VB)
Private Function LoadMyControl() As myCustomControl1
Return Page.LoadControl("....")
End Function
Then...
For each [....]
Dim myCtrl1 as myCustomControl1 LoadMyControl()
myCtrl1.prop1 = "blah"
myPlaceholder1.Controls.Add(myCtrl1)
Next
By encapsulating it within a function call the cache engine thinks it's the same user control (which it is) and will only access the disk once, speeding it up significantly.
精彩评论