Solved

ASP.net Add ImageButton or Button Template field to GridView dynamically

Posted on 2016-09-21
2
76 Views
Last Modified: 2016-09-21
Hi
I use the code below to add an image field to a GridView in ASP.net. I would like to add an image button field?
Is this possible? If not, how do I add an button template field


   Dim oGridView As New GridView
                         oGridView.ID = "Control" & CStr(oCount)
                         oGridView.Style("Width") = "100%"

                         oGridView.Attributes.Add("onkeydown", "return (event.keyCode!=13);")

                         Dim bImageField As New ImageField()
                         bImageField.DataImageUrlField = "Text1"
                         oGridView.Columns.Add(bImageField)

                         oLoadGrid(oGridView, "Select * From Test2")

                         Me.Panel_Controls.Controls.Add(oGridView)
0
Comment
Question by:murbro
2 Comments
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41809963
try create a separate class like this:
Public Class GridViewTemplate
    Implements ITemplate

    Private templateType As DataControlRowType
    Private columnName As String
    Private columnNameBinding As String
    Private controlType As String
    Private columnDirectValue As String

    Public Sub New(ByVal type As DataControlRowType, ByVal colname As String, ByVal colNameBinding As String, ByVal ctlType As String, Optional ByVal colDirectValue As String = Nothing)
        templateType = type
        columnName = colname
        columnNameBinding = colNameBinding
        controlType = ctlType
        columnDirectValue = colDirectValue
    End Sub

    Private Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
        Select Case templateType
            Case DataControlRowType.Header
                Dim lc As New Literal()
                lc.Text = columnName
                container.Controls.Add(lc)
                Exit Select
            Case DataControlRowType.DataRow
                If controlType = "Label" Then
                    Dim lb As New Label()
                    lb.ID = "lb1"
                    AddHandler lb.DataBinding, AddressOf Me.ctl_OnDataBinding
                    container.Controls.Add(lb)
                ElseIf controlType = "TextBox" Then
                    Dim tb As New TextBox()
                    tb.ID = "tb1"
                    AddHandler tb.DataBinding, AddressOf Me.ctl_OnDataBinding
                    container.Controls.Add(tb)
                ElseIf controlType = "Image" Then
                    Dim img As New Image()
                    img.ID = "img1"
                    AddHandler img.DataBinding, AddressOf Me.ctl_OnDataBinding
                    container.Controls.Add(img)
                End If
                Exit Select
            Case Else
                Exit Select
        End Select
    End Sub

    Public Sub ctl_OnDataBinding(ByVal sender As Object, ByVal e As EventArgs)
        If sender.[GetType]().Name = "Label" Then
            Dim lb As Label = DirectCast(sender, Label)
            Dim container As GridViewRow = DirectCast(lb.NamingContainer, GridViewRow)
            If columnDirectValue = Nothing Then
                lb.Text = DirectCast(container.DataItem, DataRowView)(columnNameBinding).ToString()
            Else
                lb.Text = columnDirectValue
            End If
        ElseIf sender.[GetType]().Name = "TextBox" Then
            Dim tb As TextBox = DirectCast(sender, TextBox)
            Dim container As GridViewRow = DirectCast(tb.NamingContainer, GridViewRow)
            If columnDirectValue = Nothing Then
                tb.Text = DirectCast(container.DataItem, DataRowView)(columnNameBinding).ToString()
            Else
                tb.Text = columnDirectValue
            End If
        ElseIf sender.[GetType]().Name = "Image" Then
            Dim img As Image = DirectCast(sender, Image)
            Dim container As GridViewRow = DirectCast(img.NamingContainer, GridViewRow)
            If columnDirectValue = Nothing Then
                img.ImageUrl = DirectCast(container.DataItem, DataRowView)(columnNameBinding).ToString()
            Else
                img.ImageUrl = columnDirectValue
            End If
        End If
    End Sub
End Class

Open in new window


then add the codes below to your existing codes:
        Dim bItmTemplate As New TemplateField()
        bItmTemplate.HeaderTemplate = New GridViewTemplate(DataControlRowType.Header, "HeaderText", "", "")
        bItmTemplate.ItemTemplate = New GridViewTemplate(DataControlRowType.DataRow, "", "", "Image", "images/test1.png")

        oGridView.Columns.Add(bItmTemplate)

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 41810103
Thanks very much
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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 (http://www.ecb.europa.eu/stats/exch…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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