Solved

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

Posted on 2016-09-21
2
53 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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

18 Experts available now in Live!

Get 1:1 Help Now