Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AddEventHandler for ImageButton in datagrid

Status
Not open for further replies.

harmmeijer

Programmer
Mar 1, 2001
869
CN
This is the aspx:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<HTML>
<HEAD>
<title>test</title>
</HEAD>
<body MS_POSITIONING=&quot;GridLayout&quot;>
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<asp:datagrid id=&quot;dgPlanner&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot; Width=&quot;110px&quot;>
<Columns>
<asp:BoundColumn DataField=&quot;Fname&quot; HeaderText=&quot;Name&quot;></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton id=&quot;imgBut&quot; Runat=&quot;server&quot; ImageUrl=&quot;somefile.gif&quot;>
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<asp:Label id=&quot;Label1&quot; style=&quot;Z-INDEX: 101; LEFT: 298px; POSITION: absolute; TOP: 202px&quot; runat=&quot;server&quot; Width=&quot;127px&quot;>test label</asp:Label>
</form>
</body>
</HTML>

This is the code behind (the red part is executed without errrors but the event is never triggered when I press the imagebutton)

Public Class test
Inherits System.Web.UI.Page
Protected WithEvents dgPlanner As System.Web.UI.WebControls.DataGrid
Protected WithEvents Label1 As System.Web.UI.WebControls.Label

' ... web form designer code part

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tbl As New System.Data.DataTable(&quot;table1&quot;)
Dim col As System.Data.DataColumn = New System.Data.DataColumn()
Dim rw As System.Data.DataRow
col.DataType = System.Type.GetType(&quot;System.String&quot;)
col.ColumnName = &quot;Fname&quot;
col.DefaultValue = &quot;Fname&quot;
tbl.Columns.Add(col)
rw = tbl.NewRow()
rw(&quot;Fname&quot;) = &quot;myname&quot;
tbl.Rows.Add(rw)
rw = tbl.NewRow()
rw(&quot;Fname&quot;) = &quot;other name&quot;
tbl.Rows.Add(rw)
dgPlanner.DataSource = tbl
dgPlanner.DataBind()
End Sub

Private Sub ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dgPlanner.ItemDataBound
Dim intRow As Int16 = e.Item.ItemIndex
Dim tmpObj As Object
Dim imgButInstance As ImageButton
If intRow > -1 Then
Try
tmpObj = e.Item.FindControl(&quot;imgBut&quot;)
If IsNothing(tmpObj) Then
tmpObj = Nothing
Else
imgButInstance = CType(tmpObj, ImageButton)
imgButInstance.CommandArgument = intRow
' allthough the following line attaches the imgButEdit_Click sub
' to the click event of the imagebutton it does not fire the event
imgButInstance.GetType.GetEvent(&quot;Click&quot;).AddEventHandler(imgButInstance, _
New ImageClickEventHandler(AddressOf _
Me.imgButEdit_Click))

End If
Catch exx As Exception
Dim i As Integer = 1
End Try
End If
End Sub
Public Sub imgButEdit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
Me.Label1.Text = &quot;Image button clicked on row: &quot; & sender.CommandArgument
End Sub

End Class



I know that when I change the line:

<asp:ImageButton id=&quot;imgBut&quot; Runat=&quot;server&quot; ImageUrl=&quot;somefile.gif&quot;>

to:

<asp:ImageButton onClick=&quot;imgButEdit_Click&quot; id=&quot;imgBut&quot; Runat=&quot;server&quot; ImageUrl=&quot;somefile.gif&quot;>

It works but I want to know why AddEventHandler does not work, the CommandArgument is set in the same bit of code and that works just fine.



Greetings, Harm Meijer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top