Solved

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

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTML - Color not displaying correctly in EMAIL. 6 41
Help with exporting to excel 4 28
.NET 2008 VB and C# 6 28
Sending receiving text messages in vb.net 15 38
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

822 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