ImageButton Event won't fire inside Template column of DataGrid

K

KJ

I have a custom datagrid control created with CreateChildControls()

My Image Button Event will not fire so I cannot raise it to the webform.

The ItemClick Event will not fire.

My code is below:



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"
dgItems.AllowSorting = True

' 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("MyStock")
dgItems.DataBind()
dgItems.EnableViewState = False
Controls.Add(dgItems)

End Sub

End Class

Public Class DataGridTemplate
Implements ITemplate
Dim ib As New ImageButton()
Dim templateType As ListItemType
Dim columnName As String

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.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)
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.
 
S

Stevie_mac

Did you checkout ItemCommand(...)

Any imagebutton in a template col should raise this event. In the template
editor, set the image's CommandName property to "Img" then check
e.CommandName in the DataGrids ItemCommand event.

Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.ItemCommand
If e.CommandName = "Img" Then
Response.Write("Img Clicked")
End If
End Sub
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top