harmmeijer
Programmer
This is the aspx:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>test</title>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="dgPlanner" runat="server" AutoGenerateColumns="False" Width="110px">
<Columns>
<asp:BoundColumn DataField="Fname" HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton id="imgBut" Runat="server" ImageUrl="somefile.gif">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 298px; POSITION: absolute; TOP: 202px" runat="server" Width="127px">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("table1"
Dim col As System.Data.DataColumn = New System.Data.DataColumn()
Dim rw As System.Data.DataRow
col.DataType = System.Type.GetType("System.String"
col.ColumnName = "Fname"
col.DefaultValue = "Fname"
tbl.Columns.Add(col)
rw = tbl.NewRow()
rw("Fname" = "myname"
tbl.Rows.Add(rw)
rw = tbl.NewRow()
rw("Fname" = "other name"
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("imgBut"
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("Click".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 = "Image button clicked on row: " & sender.CommandArgument
End Sub
End Class
I know that when I change the line:
<asp:ImageButton id="imgBut" Runat="server" ImageUrl="somefile.gif">
to:
<asp:ImageButton onClick="imgButEdit_Click" id="imgBut" Runat="server" ImageUrl="somefile.gif">
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>test</title>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="dgPlanner" runat="server" AutoGenerateColumns="False" Width="110px">
<Columns>
<asp:BoundColumn DataField="Fname" HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton id="imgBut" Runat="server" ImageUrl="somefile.gif">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 298px; POSITION: absolute; TOP: 202px" runat="server" Width="127px">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("table1"
Dim col As System.Data.DataColumn = New System.Data.DataColumn()
Dim rw As System.Data.DataRow
col.DataType = System.Type.GetType("System.String"
col.ColumnName = "Fname"
col.DefaultValue = "Fname"
tbl.Columns.Add(col)
rw = tbl.NewRow()
rw("Fname" = "myname"
tbl.Rows.Add(rw)
rw = tbl.NewRow()
rw("Fname" = "other name"
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("imgBut"
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("Click".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 = "Image button clicked on row: " & sender.CommandArgument
End Sub
End Class
I know that when I change the line:
<asp:ImageButton id="imgBut" Runat="server" ImageUrl="somefile.gif">
to:
<asp:ImageButton onClick="imgButEdit_Click" id="imgBut" Runat="server" ImageUrl="somefile.gif">
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