Another article in the offing...hopefully...if I get round to it...Member methods versus OnItemDataBound for nested Data List controls (English)

Another article in the offing...hopefully...if I get round to it...Member methods versus OnItemDataBound for nested Data List controls

Friday, 20 February 2004

//

1 minute read

A very common question in the ASP.NET Forumsis "How do I nest one Repeater (or DataList or DataGrid) inside of another?"; well, my usual stock answer used to be to use the OnItemDataBound event then use a FindControl() to find the Repeater inside the template and set it's datasource to the child view - then just DataBind...Recently though, I've gone against this - based in part on a bit in a book by Farhan Muhammad and Matt Milner (can't find the link to the exact book, but this oneis pretty useful too), essentially, it involves using a member method. So, instead of hooking up the event you simply specify the DataSource of the Child Repeater as follows (for example)

<asp:Repeater id=FormatsRepeater DataSource="<%# GetFormats(Container.DataItem)%>" Runat="Server">

, along with the rest of the templates etc for the child repeater. Then, in code (the codebehind normally), you simply do this:

public DataView GetFormats(object DataItem) { DataRowView di = DataItem as DataRowView; if(di!=null) return di.CreateChildView("JobCount"); else return null; }

Hmm..BlogJet isn't great at letting me modify HTML - I'll sort this later...anyway, as you can see, very simple code (incidentally, should mention, I'm using a DataSet with a DataRelation called JobCount). What's the upshot of all of this...well, the Member Method version, as well as (IMHO) being much easier to write than the ItemDataBound version - is also MUCH faster - in some tests, twice as fast! Anyway, I will hopefully write a bigger piece on this with some stats to prove my point - if I get round to it :-)

logo

© 2025 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.