TemplateBuilder

J

Jerry C

I am trying to use the template builder in a Web part. this code runs but no
control in the Template column. this is the CreateChildControls Sub from Form
4 of a sample from MSDN "Introducing the ASP.NET 2.0 Web Parts Fromework"
modifyed to try to add a grid view and custom columns to the control. The
grid shows ok and there is data in it and the bound field and Connand field
is OK.

This is the code.

Protected Overrides Sub CreateChildControls()
'Create the GridView
Controls.Clear()
grdTree = New GridView
grdTree.AutoGenerateColumns = True
grdTree.EmptyDataText = "There is no data today"
grdTree.ID = "gvTree"
grdTree.Attributes.Add("runat", "server")
grdTree.Width = 100%

'Try the TemplateField field is created and the header Text is OK
'but there is no DropDownList box in the field.

Dim grdTF As New TemplateField
grdTF.HeaderText = "Checkbox Template Field"
Dim ddlBox As New DropDownList
ddlBox.ID = "ddltop"
ddlBox.Visible = True
ddlBox.Items.Add("One")
ddlBox.Items.Add("Two")
Dim CT As New Control
CT.Controls.Add(ddlBox)
Dim grdTB As New TemplateBuilder
grdTF.ItemTemplate = grdTB
grdTF.ItemTemplate.InstantiateIn(CT)
grdTree.Columns.Add(grdTF)

'CommandField works OK.
Dim grdCF As CommandField = New CommandField
grdCF.ShowSelectButton = True
grdTree.Columns.Add(grdCF)

'BoundField works OK.
Dim grdBF As BoundField = New BoundField
grdBF.DataField = "ProductID"
grdTree.Columns.Add(grdBF)

Dim productTable As New DataTable()
Dim dad As New SqlDataAdapter(selectString, _
connectionString)
dad.Fill(productTable)
grdTree.DataSource = productTable
grdTree.DataBind()
Me.Controls.Add(grdTree)

End Sub

Thank you,
 
S

Steven Cheng[MSFT]

Hello Jerry,

From your description, I got that you're developing a custom
TemplateBuilder for GridView and the GridView and templatebuilder(template)
will be creaetd programmatically in your web part's control constructing
code, correct?

Based on the code fragment you provided, I found that you manually call the
TemplateBuilder.InstantiateIn method and pass child controls into it.
Actually, this method is designed to be invoke by ASP.NET runtime rather
than ourself. For your scenario, you want to create a custom Templateclass
for GridView TemplateField, you can considier the following options:

1. Create a custom class which implements the ITemplate interface, and
override the InstantiateIn method, put the child control constructing code
in it. For example:

==============================
Public Class DropdownTemplate
Implements ITemplate


Public Sub InstantiateIn(ByVal container As System.Web.UI.Control)
Implements System.Web.UI.ITemplate.InstantiateIn

Dim ddlBox As New DropDownList
ddlBox.ID = "ddltop"
ddlBox.Visible = True
ddlBox.Items.Add("One")
ddlBox.Items.Add("Two")

container.Controls.Add(ddlBox)

End Sub
End Class
==================================================


Then, in the page, you can use the following code to construct this custom
Template and assign it to TemplateField's ItemTemplate:

===========
Dim tf1 As New TemplateField

tf1.HeaderText = "Custom Template"
tf1.ItemTemplate = New DropdownTemplate()

grid.Columns.Add(tf1)
===================



2. Create a whole custom GridView Field which dervied from
"DataControlField" class. e.g

===============================
Public Class MyDropdownField
Inherits DataControlField


Protected Overrides Function CreateField() As
System.Web.UI.WebControls.DataControlField

Return New MyDropdownField

End Function

Public Overrides Sub InitializeCell(ByVal cell As
System.Web.UI.WebControls.DataControlFieldCell, ByVal cellType As
System.Web.UI.WebControls.DataControlCellType, ByVal rowState As
System.Web.UI.WebControls.DataControlRowState, ByVal rowIndex As Integer)

Select Case cellType
Case DataControlCellType.Header

cell.Text = Me.HeaderText

Case DataControlCellType.Footer

Case DataControlCellType.DataCell

If rowState = DataControlRowState.Normal Or rowState =
DataControlRowState.Alternate Then

Dim ddlBox As New DropDownList
ddlBox.ID = "ddltop"
ddlBox.Visible = True
ddlBox.Items.Add("One")
ddlBox.Items.Add("Two")

cell.Controls.Add(ddlBox)
End If


End Select


End Sub
End Class
========================================


Then, we can simply construct and add this class's instance into GridView's
columns collection. e.g.

===============
Dim tf2 As New MyDropdownField
tf2.HeaderText = "My Custom TemplateField"

grid.Columns.Add(tf2)

=======================


In addition, there are some other ways to create custom template (such as
load from usercontrol) in asp.net. see below:

http://msdn.microsoft.com/msdnmag/issues/05/06/CuttingEdge/default.aspx

And here is another article describing hwo to create a custom template
column. Though it is writen for ASP.NET 1.1 datagrid, it also applies to
GridView

#Creating Custom Columns for the ASP.NET Datagrid
http://msdn.microsoft.com/library/en-us/dnaspp/html/creatingcustomcolumns.as
p?frame=true


Please feel free to let me know if you have any other questions or concerns.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top