Tutorials on Itemplate

Posted on 2006-04-03
Last Modified: 2011-09-20
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.
Question by:Chaffe
    LVL 96

    Expert Comment

    by:Bob Learned
    LVL 96

    Accepted Solution

    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

      End Sub

      Public Property AutoPostBack() As Boolean
          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
          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


    Author Comment

    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.
    LVL 96

    Expert Comment

    by:Bob Learned
    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.


    Author Comment

    Bob, thanks for the help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now