Question ImageButton Sorting

herrjj

New member
Joined
Jul 21, 2008
Messages
2
Programming Experience
5-10
I have a DataGrid that I want to sort when a ImageButton in the column header is clicked. The column appears perfectly along with the ImageButton:

PHP:
<asp:TemplateColumn SortExpression="Fullname DESC">
	<HeaderStyle Width="25%" VerticalAlign="Middle"></HeaderStyle>
	<ItemStyle Width="25%"></ItemStyle>
	<HeaderTemplate>
		<nobr>Full Name
			<asp:ImageButton id="btnSortFullname" runat="server" CommandName="sort" ImageUrl="images/sort.png"
				Width="28" Height="15" CommandArgument="Fullname DESC" ImageAlign="AbsMiddle" />
		</nobr>
	</HeaderTemplate>
	<ItemTemplate>
		<%# DataBinder.Eval(Container.DataItem, "Fullname") %>
	</ItemTemplate>
</asp:TemplateColumn>

On the backend, I have the following event handler code:

PHP:
Private Sub DataGrid1_SortCommand(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
    SortField = e.SortExpression
    BindDataGrid()
End Sub

I placed a breakpoint in my event handler code and found that clicking the ImageButton does not cause the event to fire. If I replace the ImageButton with a LinkButton with the same CommandName arguement, the event is fired and the breakpoint is reached:

PHP:
<asp:LinkButton runat="server" CommandName="sort" CommandArgument="Fullname DESC" ID="Linkbutton1">My LinkButton</asp:LinkButton>

Did I miss a step? All the docs I've read at MSDN and VB.Net Help indicate to do it as shown above.

Thanks!
 
So, I found a workaround but would rather have the solution to the original issue if one exists.

For now, I added an OnClick parameter to each ImageButton:

PHP:
<asp:ImageButton OnClick="headerSort" id="btnSortFullname" runat="server" CommandName="sort" ImageUrl="images/sort.png" Width="28" Height="15" CommandArgument="Fullname DESC" ImageAlign="AbsMiddle" />

In the code behind, the headerSort Sub is defined as:

PHP:
Public Sub headerSort(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Dim imgBtn As ImageButton = CType(sender, ImageButton)
        SortField = imgBtn.CommandArgument
        BindDataGrid()
End Sub

In affect, I'm bypassing the event handler of the DataGrid and capturing the sort parameters myself. From there it's just a matter of applying the sort and re-binding the grid.
 
Back
Top