12 June 2007

Custom Objects

Say you have a custom object MyObject and you have a custom collection MyObjectCollection inheriting from CollectionBase so that you are able to bind it to a repeater. Lets say you have a custom web control that has a MyObj property that you try to set in the code front like so:

MyControl MyObj = < %# Container.DataItem %>

This does not work for me, although people have said it works for them. So after MUCH googling I found a workaround, besides loading the control in the codebehind and doing a Placeholder.Controls.Add(mycontrol) while foreaching through each object in the collection.

First add this to your repeater:


Then set the codebehind like so:

public void repeater_itemcreated(object sender, RepeaterItemEventArgs e)




    if((e.Item.Controls.Count>1) && (e.Item.Controls[1].GetType().Name == "usercontrols_mycontrol_ascx"))




        ((UserControls_MyControl)e.Item.Controls[1]).MyObjProperty = (MyObj)e.Item.DataItem;


        ((UserControls_MyControl)e.Item.Controls[1]).ID = "Content";





Labels: , , , ,