Solved

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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