Image Button inside a template column - Handling The Click Event

K

KJ

I have been searching for 2 days on how to handle a imagebutton click
event inside a template column on a datagrid control. Does anyone know
how to do it?

The ItemClick Event does not fire when I click the image button
therefore the event never gets raised to the webform. Please Help

My code is below





Public Class DataGridTemplate
Implements ITemplate
Dim templateType As ListItemType
Dim columnName As String

Event ItemImageClick(ByVal source As Object, ByVal e As
System.Web.UI.ImageClickEventArgs)

Sub New(ByVal type As ListItemType, ByVal ColName As String)
templateType = type
columnName = ColName
End Sub

Sub InstantiateIn(ByVal container As Control) _
Implements ITemplate.InstantiateIn
ib = New ImageButton()

ib.ImageUrl = Select.gif"
ib.ToolTip = "Select Item"
ib.ImageAlign = ImageAlign.AbsBottom
ib.CommandName = "Select"
AddHandler ib.Click, AddressOf ItemClick
container.Controls.Add(ib)

End Sub

Public Sub ItemClick(ByVal sender As Object, _
ByVal e As
System.Web.UI.ImageClickEventArgs)
RaiseEvent ItemImageClick(Me, e)

End Sub
 
S

Severin

On an ASPX page, only PAGE level controls have events...

When you place any other <asp:> control inside a datagrid, it is no longer a
PAGE level control, it is now a CHILD of the datagrid.

So... You must use the DataGrid event 'ItemCommand' for all events that can
be raised from internal controls.
Using the ImageButton CommandArguement and CommandName to determine which
imagebutton was cliked.

Usually you would put the primaryKey of the datatable in the CommandArgument
property, and then any string for the CommandName property

example below:
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.ItemCommand

If e.CommandName = [property of ImageButton] Then

Dim primaryKey as Integer = CInt(e.CommandArguement)

' Do logic or database command for image clicked event

End If

End Sub

Severin
 
K

Kyle Johnson

My dgItemClick Event that handles dgItems.ItemCommand is not firing.
Does anyone know why?

Below is the code:



Public Class ABC
Inherits System.Web.UI.WebControls.WebControl

Dim dgItems As New DataGrid()
Dim x As New TemplateColumn()
Dim y As DataGridColumn()
Dim s As New BoundColumn()
Dim k As New BoundColumn()
Dim l As New BoundColumn()
Dim e As New BoundColumn()
Dim strsql As String

Protected Overrides Sub CreateChildControls()

dgItems.CellPadding = 2
dgItems.CellSpacing = 0
'dgItems.Width = 550px
' dgItems.BorderWidth = 1
dgItems.BorderColor = ColorTranslator.FromHtml("Black")
dgItems.AutoGenerateColumns = False
dgItems.ForeColor = ColorTranslator.FromHtml("Black")
' dgItems.Font.Size = 8
dgItems.Font.Name = "Arial"

' x.ItemStyle.BackColor = ColorTranslator.FromHtml("#FFF778")
x.ItemStyle.HorizontalAlign = HorizontalAlign.Center
s.HeaderText = "hasccp"
s.DataField = "hasccp"

k.HeaderText = "Number"
k.DataField = "Number"

l.HeaderText = "Type"
l.DataField = "Type"

e.HeaderText = "Name"
e.DataField = "Name"

x.ItemTemplate = New DataGridTemplate(ListItemType.Item,
"Column1")
dgItems.Columns.AddAt(0, x)
dgItems.Columns.AddAt(1, s)
dgItems.Columns.AddAt(2, k)
dgItems.Columns.AddAt(3, l)
dgItems.Columns.AddAt(4, e)
dgItems.DataSource = RcpDS.Tables("Recipes")
dgItems.DataBind()
dgItems.EnableViewState = True
Controls.Add(dgItems)
'Controls.Add(New LiteralControl("</div>"))
AddHandler dgItems.ItemCommand, AddressOf dgItemClick

End Sub
Public Sub dgItemClick(ByVal sender As Object, _
ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
If e.CommandName = "Select" Then
End If
End Sub

End Class
 
K

KJ

Through much searching.


I found the solution. It was not as clearing spelled out either.

One event that does always fire is the datagrid DataBinding event.


1. Inside the databinding event. You have to call EnsureChildControls
method. Ex.

Private Sub datagrid1_DataBinding(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dgItems.DataBinding
EnsureChildControls()
End Sub

2. In the Page_Load of your web form this is where you call your
public method to bind the data to your datagrid. In your control
create a method that just creates the dataset, sets it to the
datasource of the grid and then binds it. Ex.


Public Sub BindDataGrid()
Dim sqlconn As New SqlClient.SqlConnection()
sqlconn.ConnectionString = "data source="
sqlconn.Open()

strsql = "select * from google"

Dim sqlCommand As New SqlClient.SqlCommand(strsql, sqlconn)
sqlCommand.CommandType = CommandType.Text

Dim rcpDA As SqlClient.SqlDataAdapter = New
SqlClient.SqlDataAdapter()
rcpDA.SelectCommand = sqlCommand

Dim RcpDS As DataSet = New DataSet()
rcpDA.Fill(RcpDS, "Searches")
datagrid1.DataSource = RcpDS.Tables("Searches")
datagrid1.DataBind()

End Sub


Call this from the Page_Load of the webform.

All your events should fire now. Hope this helps. Sorry I cannot give
a technical explaination for this but maybe someone will reply with
one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top