ImageButton in column's HeaderTemplate doesn't generate Click event

S

Stephen Miller

I have a datagrid, with a template column containing a CheckBox called
chkSelect in the ItemTemplate. Users select rows to edit/delete by
checking the appropriate checkbox.

I want to add a simple image button to the HeaderTemplate that enables
users to auto select all rows (ie Hotmail):

<asp:TemplateColumn headertext="">
<HeaderTemplate>
<asp:ImageButton id="imgCheck" runat="server"
ImageUrl="check.gif"></asp:ImageButton>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="chkSelect" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

The code behind for the ImageButton's Click event is simply:

Private Sub imgCheck_Click(ByVal sender As System.Object, _
ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgCheck.Click

Dim myItem As DataGridItem
Dim myChkBox As CheckBox

For Each myItem In myDataGrid.Items
myChkBox = CType(myItem.Cells(0).FindControl("chkSelect"), CheckBox)
myChkBox.Checked = True
Next
End Sub

The problem is that the ImageButton's Click event is not being called
when nested in the column's HeaderTemplate. If I move the ImageButton
and place it elsewhere on the page, the code work's fine.

What am I doing wrong?

Thanks,

Stephen
 
J

Jos

Stephen said:
I have a datagrid, with a template column containing a CheckBox called
chkSelect in the ItemTemplate. Users select rows to edit/delete by
checking the appropriate checkbox.

I want to add a simple image button to the HeaderTemplate that enables
users to auto select all rows (ie Hotmail):

<asp:TemplateColumn headertext="">
<HeaderTemplate>
<asp:ImageButton id="imgCheck" runat="server"
ImageUrl="check.gif"></asp:ImageButton>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="chkSelect" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

The code behind for the ImageButton's Click event is simply:

Private Sub imgCheck_Click(ByVal sender As System.Object, _
ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgCheck.Click

Dim myItem As DataGridItem
Dim myChkBox As CheckBox

For Each myItem In myDataGrid.Items
myChkBox = CType(myItem.Cells(0).FindControl("chkSelect"), CheckBox)
myChkBox.Checked = True
Next
End Sub

The problem is that the ImageButton's Click event is not being called
when nested in the column's HeaderTemplate. If I move the ImageButton
and place it elsewhere on the page, the code work's fine.

What am I doing wrong?

Thanks,

Stephen

You forgot to set the button's OnClick event to be handled by your
imgCheck_Click handler.
Just add OnClick="imgCheck_Click" to the ImageButton.
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top