ITPub博客

首页 > 应用开发 > IT综合 > Using the Template Collection to Add a Template Column in WebDataGrid

Using the Template Collection to Add a Template Column in WebDataGrid

原创 IT综合 作者:yuzhangqi 时间:2011-01-04 16:49:34 0 删除 编辑

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

http://help.infragistics.com/Help/NetAdvantage/ASPNET/2009.1/CLR3.5/html/WebDataGrid_Using_the_Template_Collection.html

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13651903/viewspace-1044090/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-06-10

  • 博文量
    102
  • 访问量
    736325