Using the Template Collection to Add a Template Column in WebDataGrid

yuzhangqi發表於2011-01-04

WebDataGrid™ has a template collection to store common, reusable templates. You can add item templates to the collection using the WebDataGrid control's smart tag. You can also do this in code using the Templates collection.


You can use the templates as you would for any templatable element in WebDataGrid. The collection gives you a place to store and quickly access templates that you plan to use often.

Next I will show you how to add template column programmaticly.

1. Define your custom Tempate

Public Class CheckBoxColumnTemplate
Implements System.Web.UI.ITemplate

Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
Dim checkBox As New CheckBox()

checkBox.ID = "CheckBox1"
checkBox.Checked = False
checkBox.Attributes.Add("onclick", "selectFirstColumn(event);")
container.Controls.Add(checkBox)

End Sub

End Class

Public Class LinkButtonColumnTemplate
Implements System.Web.UI.ITemplate

Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
Dim linkButton As New LinkButton()
linkButton.ID = "LinkButton1"
container.Controls.Add(linkButton)

End Sub

End Class

2. Define a Enum for Template Type

Public Enum GridViewColumn
Text
CheckBox
LinkButton
CheckBoxWithEvent
End Enum

3. Create Template Column

Public Shared Function CreateTemplateDataField(ByVal key As String, ByVal headerText As String, ByVal width As Integer, ByVal hidden As Boolean) As TemplateDataField

Return CreateTemplateDataField(key, headerText, width, hidden, GridViewColumn.CheckBox)

End Function

Public Shared Function CreateTemplateDataField(ByVal key As String, ByVal headerText As String, ByVal width As Integer, ByVal hidden As Boolean, ByVal colType As GridViewColumn) As TemplateDataField
Dim column As New TemplateDataField()
Dim itemTemplate As System.Web.UI.ITemplate

Select Case colType
Case GridViewColumn.CheckBox
itemTemplate = New CheckBoxColumnTemplate()
Case GridViewColumn.Text
itemTemplate = New TextColumnTemplate()
Case GridViewColumn.LinkButton
itemTemplate = New LinkButtonColumnTemplate()
Case GridViewColumn.CheckBoxWithEvent
itemTemplate = New CheckBoxWithEventColumnTemplate()
Case Else
itemTemplate = New CheckBoxColumnTemplate()
End Select

column.ItemTemplate = itemTemplate

column.Key = key
column.Header.Text = headerText
column.Width = width
column.Hidden = hidden

Return column
End Function

4. Create a Bound Column

Public Shared Function CreateBoundDataField(ByVal key As String, ByVal headerText As String, ByVal width As Integer, ByVal hidden As Boolean) As BoundDataField
Dim column As New BoundDataField()
column.Key = key
column.DataFieldName = key
column.Header.Text = headerText
column.Width = width
column.Hidden = hidden

Return column
End Function

5. Put All Together

With childGrid
.Columns.Add(Common.CreateTemplateDataField("CheckBoxs", "", 20, False, GridViewColumn.CheckBox))
.Columns.Add(Common.CreateBoundDataField("ID", GlobalResources.GetString("GridHeader_ID"), 60, True))

End With

References

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13651903/viewspace-1044090/,如需轉載,請註明出處,否則將追究法律責任。

相關文章