.NET Data Secrets, Part II
Little Known Technique for Confirming Web Deletes
Simply add the following code behind your delete button:
MyDeleteButton.Attributes("onclick") = _ "return confirm('Are you sure you wish to _ delete this record?');"
However, this doesn't accommodate those developers using actual delete buttons in DataGrid columns. In such a situation, you need to catch the individual delete buttons as the data bind occurs and add this 'onclick' attribute.
The following snippet of code assumes you have a Web DataGrid with Delete buttons in the first column (index zero). Note that it currently explicitly checks for a Button control, not a PushButton (ensure the 'Button type' is set correctly in the Property Builder, or alter the code). Simply add the following to respond to the ItemDataBound event of the DataGrid control, ready to fire when any data bind occurs:
If e.Item.ItemType = ListItemType.Item Or _ e.Item.ItemType = ListItemType.AlternatingItem Then If e.Item.Cells(0).Controls.Count > 0 Then If TypeOf (e.Item.Cells(0).Controls(0)) Is Button Then Dim btnDelete As Button = CType(e.Item.Cells(0). _ Controls(0), Button) btnDelete.Attributes("onclick") = _ "return confirm('Are you sure you wish to _ delete this record?');" End If End If End If
And that's it: simple, effective, and pretty hush-hush.
Figure 2: Confirm your Web deletes in a Web grid, easily!
Selecting Multiple Web Form Grid Items, Hotmail-Style
Selecting and, say, deleting items one-by-one can be a real pain. E-mail services such as Hotmail and Yahoo! Mail recognize that and allow you to select multiple messages through a little checkbox, then zap them all at once. Well, anything Hotmail can do, we can do ... quicker.
To create a selection checkbox, first set up your Web DataGrid as normal—displaying anything from order information through to mail messages. Next, right-click and select Property Builder. Choose the Columns property sheet and add a 'Template Column' to the list of selected columns, moving it to the top of the list (the first column). Set the 'Header text' value if you wish. Click OK when finished.
Back on your Web page, right-click your DataGrid again, this time choosing Edit Template, then choosing your new Template Column. This is your template for this particular field. Drag-and-drop a CheckBox control into the ItemTemplate portion, changing its ID property to 'chkSelect'. When finished, right-click on the DataGrid again and select 'End Template Editing'. You should be able to see the difference on your DataGrid.
Next, add a button to your Web Form. This will be the button your user clicks after selecting records to delete (or perform some other action upon). Add code behind the button Click event, similar to the following:
Dim objItem As DataGridItem For Each objItem In MyDataGrid.Items ' Ignore invalid items If objItem.ItemType <> ListItemType.Header And _ objItem.ItemType <> ListItemType.Footer And _ objItem.ItemType <> ListItemType.Pager Then ' Retrieve the value of the check box Dim blnDelete As Boolean blnDelete = CType(objItem.Cells(0).FindControl("chkSelect"), _ CheckBox).Checked If blnDelete = True Then ' Delete this row from the underlying DataSet, ie. ' LocalDS.Tables(0).Rows(MyDataGrid.SelectedIndex).Delete ' You can also retrieve the value of a field on the row, ie. ' MyVariable = objItem.Cells(5).Text ' ... then rebind. End If End If Next
Here, our code walks through each valid item in the DataGrid, searching for our control in the first cell (zero index) and analyzing whether it's checked. If it is, that's where your code can step in to take action—probably deleting the record in the underlying DataSet, then rebinding, as per the examples in the "Nine Steps to a Quick, Editable Web Grid" tip.
And that's it. You now should be able to select multiple cells and perform an en masse operation, such as a delete, in seconds! You may even want to merge this tip with the next for even more power over your data...
Figure 3: Selecting multiple items in our Web grid, Hotmail-style
Click Anywhere and Select, with a Web Grid
Web applications are not like Windows applications. We know that. But, by using tricks such as the SmartNavigation property we covered in the last chapter, you can give your sites more intelligence, allowing them to be much more responsive, to work better.
This next tip adds to that repertoire. By using the following code, you can click anywhere in a DataGrid and have the record you were over selected (or, rather, have your code behind the SelectedIndexChanged event run). Especially useful for those with a speedy Internet connection, or using an Intranet site, where postbacks are hardly noticed.
Anyway, here's the code. It presumes the very first column contains a Select button of the PushButton variety (though you can make this column invisible through the Property Builder, if you wish to do so). It finds this Select button in code and, through the highly hush-hush GetPostBackClientHyperlink function, returns the name of the script which runs when pressed. This is then set to run when the 'onclick' event of the row is fired. Just add the following to respond to the ItemDataBound event of the DataGrid control:
If e.Item.ItemType = ListItemType.Footer Or _ e.Item.ItemType = ListItemType.Footer Or _ e.Item.ItemType = ListItemType.Pager Then Exit Sub If e.Item.Cells(0).Controls.Count > 0 Then If TypeOf (e.Item.Cells(0).Controls(0)) Is Button Then Dim btnSelect As Button = CType(e.Item.Cells(0).Controls(0), _ Button) e.Item.Attributes("onclick") = _ Page.GetPostBackClientHyperlink(btnSelect, "") End If End If
Figure 4: Click anywhere and select, with this crafty code
Coming up in part three of .NET Data Secrets:
- Quick and Easy Data Paging, with Your Web Grid
- Sorting in Seconds, with Your Web Grid
- Amazingly Simple Method for Exporting Your Web Grid to Excel
- Get Microsoft to Write Your Code—Amazing Undocumented SQL Server Tool!
Also, don't forget that you can contribute to this very series yourself. Simply send your favorite little-known .NET snippets to me—email@example.com—and I'll publish the best with full credits.
See you then!
About the Author
Karl Moore is a technology author living in Yorkshire, England. He runs his own consultancy group, White Cliff Computing Ltd, and is author of two best-selling books exposing the secrets behind Visual Basic .NET. When he's not writing for magazines, speaking at conferences, or making embarrassing mistakes on live radio, Karl enjoys a complete lack of a social life. Check out Karl's newest book, Ultimate VB .NET and ASP.NET Code Book.
# # #
Page 2 of 2