Add user controls to dynamically created Template Fields
Posted on 2009-02-18
I need to create a Grdiview based on a crosstab query, each of the columns contains a User Control in the header cell and a User Control in the row cell, the type of User Control in each column is the same down but may be diffrent for each column.
I have done this by dynamically adding Template Fields to the GridView and creating a Header Template and an Item Template. The problem is how do you load a User control in a class?
The code I have used elsewhere is:
Dim control As Object = LoadControl("Controls\MarkType\" + controlType + ".ascx")
This works fine when on a web page or within a User Control but not from within a class.
The header template contains the same User Control in every column, The way I have got this to work is by instanciating the User Control class using the following code:
Dim theControl As New UnitPartMarkHeader
This works fine except the UnitPartMarkHeader control doesn't recognise any of the objects within the control and so I have had to programitcally create all of the components within the User Control, which is fine for this control as all it contains is an ImageButton (The reason I have created it as a User Control is there are lots of properties about the column stored in the control), but for the User Controls I need to display in the rows there are 7 diffrent types of User Control and some have quite a few objects within them and I don't really want to have to change all of these to create everything dynamically, if possible.
So my question is: how do I dynamically add User Controls inside the Item Template of a dynamically created Template Field?
Thanks to anyone who can help.