This post originated from an RSS feed registered with .NET Buzz
by Doug Thews.
Original Post: Adding Image Buttons to a DataGrid
Feed Title: IlluminatiLand
Feed URL: http://apps5.oingo.com/apps/domainpark/domainpark.cgi?client=netw8744&s=JETBRAINS.COM
Feed Description: A technology blog for people enlightened enough to think for themselves
I've seen a few posts recently about how to put actionable image buttons in a column for a datagrid, so I thought I'd show an example.
The first thing to do is to add a ButtonColumn inside your DataGrid. This is a sample for a Delete button image (I use a 1% width to match the size of my image):
Assuming our DataGrid is named DataGrid1, you'll want to handle the DataGrid1.ItemCommand event. The event passes back the command name (remember DeleteRecord from our command button definition?) as the type DataGridCommandEventArgs. Here's a sample for interacting with our DeleteRecord command:
Protected Sub DataGrid1_ItemCommand(ByVal sender As System.Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.ItemCommand
' Check to see if the user selected the pager. If not, then this must be
' a command button
If (e.Item.ItemType <> ListItemType.Pager) Then
Select Case e.CommandName
Case "DeleteRecord"
' Do your thing here
End Select
Else
' This section would be where any page control stuff goes
End If
End Sub
You'll notice that we also checked to make sure that the command type was not a pager before we went off and checked command name (which would have been null in that case).
That's pretty much it. I use this in my sample HomeInventory system that I use to proof out a lot of ASP.NET tools & techniques. Check it out and let me know what you think.