• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 742
  • Last Modified:

Tutorials on Itemplate

Greetings, I build a GridView in VB to display on an aspx page, and I'm intrested in adding a TemplateField to extend some functionality.
I need to define a class that implements ITemplate to hold the content of my ItemTemplate, and I'm looking for any good tutorials that would show me how to do this sort of thing.  Thanks very much.
0
Chaffe
Asked:
Chaffe
  • 3
  • 2
1 Solution
 
Bob LearnedCommented:
0
 
Bob LearnedCommented:
Here is the translated VB.NET code:

Imports System.Data
Imports Microsoft.VisualBasic

Public Class Template
  Implements ITemplate

  Private m_checkBox As CheckBox

  Public Event CheckedChanged As EventHandler

  Private m_readOnly As Boolean = False
  Public Sub New(ByVal editable As Boolean)

    m_readOnly = Not editable

  End Sub

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

    Dim m_checkBox As New CheckBox

    AddHandler m_checkBox.DataBinding, AddressOf BindData
    AddHandler m_checkBox.CheckedChanged, AddressOf OnCheckChanged
    container.Controls.Add(m_checkBox)

  End Sub

  Public Property AutoPostBack() As Boolean
    Get
      Return m_checkBox.AutoPostBack
    End Get
    Set(ByVal value As Boolean)
      m_checkBox.AutoPostBack = value
    End Set
  End Property

  Private m_dataField As String = ""

  Public Property DataField() As String
    Get
      Return m_dataField
    End Get
    Set(ByVal value As String)
      m_dataField = value
    End Set
  End Property 'DataField

  Private Sub BindData(ByVal sender As Object, ByVal e As EventArgs)

    Dim container As DataGridItem = CType(m_checkBox.NamingContainer, DataGridItem)

    m_checkBox.Checked = False
    m_checkBox.Enabled = Not m_readOnly

    Dim data As String = CType(container.DataItem, DataRowView)(Me.DataField).ToString()
    Dim type As Type = CType(container.DataItem, DataRowView).DataView.Table.Columns(Me.DataField).DataType

    If data.Length > 0 Then
      Select Case type.ToString()
        Case "System.Boolean"
          If data = "True" Then
            m_checkBox.Checked = True
          End If
        Case Else
      End Select
    End If
  End Sub

  Public Sub OnCheckChanged(ByVal sender As Object, ByVal e As EventArgs)

    RaiseEvent CheckedChanged(sender, e)

  End Sub

End Class

Bob
0
 
ChaffeAuthor Commented:
Bob, this code is going to save me a lot of work.  thanks a bunch..
Since this is done on a dataGrid, Do you happen to know what the GridView equivilent of:
DataGridItem & DataRowView.
0
 
Bob LearnedCommented:
I am not 100% sure, but I believe that the equivalent is GridViewRow.  DataRowView should still be the same, but that depends on what type 'container.DataItem' is.

Bob
0
 
ChaffeAuthor Commented:
Bob, thanks for the help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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