Dynamically assigning imageUrl
<p>I am using below class to create dynamic template field with image button, everythink is working fine but customer wants to display different images on imagebutton based on certain values of Gridview.</p>

<p>Any idea or suggestion would be appreciated.</p>

<p>Public Class GridViewTemplate
    Implements ITemplate</p>

<pre><code>Private templateType As DataControlRowType
Private columnName As String

Sub New(ByVal type As DataControlRowType, ByVal colname As String)

    templateType = type
    columnName = colname

End Sub

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

    ' Create the content for the different row types.
    Select Case templateType

        Case DataControlRowType.Header
            ' Create the controls to put in the header
            ' section and set their properties.
            Dim lc As New Literal
            lc.Text = "&lt;b&gt;" &amp; columnName &amp; "&lt;/b&gt;"

            ' Add the controls to the Controls collection
            ' of the container.
            container.Controls.Add(lc)

        Case DataControlRowType.DataRow
            ' Create the controls to put in a data row
            ' section and set their properties.
            Dim firstName As New ImageButton
            Dim lastName As New Label

            Dim spacer = New Literal
            spacer.Text = " "

            ' To support data binding, register the event-handling methods
            ' to perform the data binding. Each control needs its own event
            ' handler.
            AddHandler firstName.DataBinding, AddressOf FirstName_DataBinding
            AddHandler lastName.DataBinding, AddressOf LastName_DataBinding

            ' Add the controls to the Controls collection
            ' of the container.
            container.Controls.Add(firstName)
            container.Controls.Add(spacer)
            container.Controls.Add(lastName)

            ' Insert cases to create the content for the other 
            ' row types, if desired.

        Case Else

            ' Insert code to handle unexpected values. 

    End Select

End Sub

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

    ' Get the Label control to bind the value. The Label control
    ' is contained in the object that raised the DataBinding 
    ' event (the sender parameter).
    Dim l As ImageButton = CType(sender, ImageButton)

    ' Get the GridViewRow object that contains the Label control. 
    Dim row As GridViewRow = CType(l.NamingContainer, GridViewRow)

    ' Get the field value from the GridViewRow object and 
    ' assign it to the Text property of the Label control.
    l.ImageUrl = DataBinder.Eval(row.DataItem, columnName).ToString()

End Sub

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

    ' Get the Label control to bind the value. The Label control
    ' is contained in the object that raised the DataBinding 
    ' event (the sender parameter).
    Dim l As Label = CType(sender, Label)

    ' Get the GridViewRow object that contains the Label control.
    Dim row As GridViewRow = CType(l.NamingContainer, GridViewRow)

    ' Get the field value from the GridViewRow object and 
    ' assign it to the Text property of the Label control.
    l.Text = DataBinder.Eval(row.DataItem, columnName).ToString()

End Sub
</code></pre>

<p>End Class</p>
