Solved

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

Posted on 2016-09-21
2
88 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 52

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

707 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