[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 757
  • Last Modified:

Add user controls to dynamically created Template Fields

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
theControl.LoadControl("Controls\UnitPartMarkHeader.ascx")

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.
0
WNottsC
Asked:
WNottsC
  • 3
  • 2
1 Solution
 
TimCotteeCommented:
Hello WNottsC,

Perhaps something like this:

Dim upmh As Control = LoadControl("controls\UnitPartMarkHeader.ascx")
With CType(upmh, controls_UnitPartMarkHeader)
  'Set any properties here              
End With
TemplatePlaceholderControl.Controls.Add(upmh)

Where the "controls_UnitPartMarkHeader" is the class name of the control and TemplatePlaceHolderControl is the relevant template container control.

Regards,

TimCottee
0
 
WNottsCAuthor Commented:
Hi,

Below is the code I use to add the template field to the GridView.

Dim bfield As TemplateField = New TemplateField
bfield.HeaderTemplate = New GridViewTemplate(ListItemType.Header, col.ColumnName)
bfield.ItemTemplate = New GridViewTemplatey(ListItemType.Item, col.ColumnName)
grdstudentMarks.Columns.Add(bfield)

Do you mean by code should be:

Dim bfield As TemplateField = New TemplateField
bfield.HeaderTemplate = New GridViewTemplate(ListItemType.Header, col.ColumnName)
bfield.ItemTemplate = New GridViewTemplatey(ListItemType.Item, col.ColumnName)

Dim upmh As Control = LoadControl("controls\UnitPartMarkHeader.ascx")
With CType(upmh, controls_UnitPartMarkHeader)
  'Set any properties here              
End With
bfield.Controls.Add(upmh)
grdstudentMarks.Columns.Add(bfield)

If so the TemplateField object doesn't contain a controls property.
Or is there somethnig other than a TemplateField I should be adding?

Thanks
0
 
TimCotteeCommented:
WNottsC,

Thinking about it, that isn't going to work effectively anyway. You are going to need to use the method but add your controls in the RowDataBound event handler for the gridview (assuming it is databound, if not then you can create a dummy datatable to bind it to to force the event to be fired and the controls to be added). The approach is the same however, in the RowDataBound you would need to add the new control to each cell in the row.

TimCottee
0
 
WNottsCAuthor Commented:
TimCottee,

I have found a way to do it, rather than using a class which extends ITemplate as the ItemTemplate for the Template Field I have modified my User Controls to extend ITemplate.
A simplified example below:

Dim bfield As TemplateField = New TemplateField
Dim itemControl As ItemControlUC = LoadTemplate("ItemControlUC.ascx")
bfield.ItemTemplate = itemControl
grdstudentMarks.Columns.Add(bfield)

The only problem I have left is how do I set properties of the itemControl?
The code below brings up the following error message:

itemControl.Property1 = "Value"
"Public Member 'Property1' on type 'SimpleTemplate' not found.

If I use LoadControl instead of LoadTemplate then it allows me to set properties of the UserControl but when I set the ItemTemplate  to be the control  nothing appears, presumably becuase it needs to be a template.

Do you know how I can set properties of the UserControl and then load it with LoadTemplate?
0
 
WNottsCAuthor Commented:
Ignore that last post, adding the items in the RowDataBound event works fine.

Thanks alot.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now