We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Add user controls to dynamically created Template Fields

Medium Priority
793 Views
Last Modified: 2013-11-26
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.
Comment
Watch Question

TimCotteeHead of Software Services

Commented:
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

Author

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
Head of Software Services
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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?

Author

Commented:
Ignore that last post, adding the items in the RowDataBound event works fine.

Thanks alot.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.