http://www.developer.com/net/asp/article.php/2210191/NET-Data-Secrets-Part-II.htm
Welcome to the second part of .NET Data Secrets! I'm Karl Moore and today we'll be exploring even more secrets to make your data access applications run as smoothly as possible—especially the Web variety: If you missed the secrets from last time, you can find them here. Let's get on with all those juicy secrets! As demonstrated in the last article, setting up your own editable Windows DataGrid may be an absolute doddle—however, creating the equivalent grid for the Web is a little more complicated. Most articles simply show you how to display data—and conveniently manage to skip the editing, deleting, adding, and updating stages. Not here. Here, you're going to create a template page that will allow you to display data from a table. You'll be able to add new records. Delete records. Edit existing records. Update the backend database. It'll handle most of your simple table operations. I've written all the code for you—and if there's any piece of functionality you don't want, just don't add it. Let's get started: From this code base, you can do practically anything using the Web DataGrid and a little imagination. You could create a form that allows you to add items through regular input boxes, then in code creates a new row in the DataGrid and fills out the relevant fields. You could modify it so the DataSet actually contains items in a user's shopping basket, with an update feature to change quantities. You could simply use it to create a power user system that allows a privileged few to access and edit data in key administration tables within your company database. In short, there are many different methods of displaying data on the Web—and everyone wants to do something slightly different. Here, I've provided a standard code framework that will allow you to perform the most commonly requested—adding, deleting, editing, and updating of records in a table. It's now up to you to customize and take this base model to new heights. The techniques that follow this introduction will be particularly useful to those who have become acquainted with the Web DataGrid and how it works. They gradually get more advanced, assuming a working knowledge of the DataGrid and ADO.NET technologies. Figure 1: A selected record in my final, editable Web grid! This next code technique is a real gem—one of those tips you rarely find printed, but when you figure out how it works, you won't be able to get enough of it. This tip shows you how to run a snippet of JavaScript to confirm an action, such as the deletion of a record, all on the client side. Best of all, it takes just one line of code. Simply add the following code behind your delete button: This adds a small piece of JavaScript to run when the client side 'onclick' event occurs for the rendered HTML button. The 'Are you sure?' prompt then appears and if it the user confirms, a True is returned and the click is processed. Otherwise, it simply cancels itself out. 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: And that's it: simple, effective, and pretty hush-hush. Figure 2: Confirm your Web deletes in a Web grid, easily! 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: 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 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: Figure 4: Click anywhere and select, with this crafty code Coming up in part three of .NET Data Secrets: Also, don't forget that you can contribute to this very series yourself. Simply send your favorite little-known .NET snippets to me—karl@karlmoore.com—and I'll publish the best with full credits. See you then! 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.
# # #
.NET Data Secrets, Part II
May 21, 2003
Nine Steps to a Quick, Editable Web Grid
Top Tip: To preview the data coming back from your DataAdapter, right-click on your DataAdapter object and select 'Preview Data'. To change what is returned (for example, to remove certain columns or add calculated fields), right-click and select 'Configure Data Adapter'; then use the designer to prepare a customized SQL statement. Do not remove primary keys: instead, make them invisible (see the next 'Top Tip').
If Not IsPostBack Then
MyDataAdapter.Fill(MyDataSet)
MyDataGrid.DataSource = MyDataSet
MyDataGrid.DataBind()
DataSave(MyDataSet)
' The DataSave function will be added later.
' Remove this line and stop here if you want
' a read-only DataGrid
End If
Top Tip: If you only want to display certain columns in the DataGrid, you can selectively choose those required through the Property Builder. Firstly, in the General property sheet, select your data by choosing your DataSet and table for the DataSource and DataMember properties. Next, in the Columns property sheet, uncheck 'Create columns automatically at run time', then move individual fields from the list of available columns (under 'Data Fields') over to the selected columns list. Click OK when finished and continue the instructions.
Dim intCount As Integer
For intCount = 1 To MyDataGrid.Items.Count
MyDataGrid.Items(intCount - 1).BorderStyle = _
BorderStyle.Groove
Next
MyDataGrid.SelectedItem.BorderStyle = BorderStyle.Dashed
Public Sub DataSave(ByVal DataSet As DataSet)
If DataExists() Then
ViewState.Item("__Data") = DataSet
Else
ViewState.Add("__Data", DataSet)
End If
End Sub
Public Function DataRetrieve() As DataSet
Return CType(ViewState.Item("__Data"), DataSet)
End Function
Public Function DataExists() As Boolean
If Not ViewState.Item("__Data") Is Nothing _
Then Return True
End Function
' Code to respond to the Click event of the ADD button:
' Desc: Adds a new row to the DataSet, rebinds to the
' DataGrid, then makes the row editable
If DataExists() = False Then Exit Sub
Dim LocalDS As DataSet = DataRetrieve()
Dim rowNew As System.Data.DataRow = LocalDS.Tables(0).NewRow
' Enter sample values for non-null fields here
' ie, rowNew.Item("uniqueTag") = "sample"
LocalDS.Tables(0).Rows.Add(rowNew)
MyDataGrid.EditItemIndex = MyDataGrid.Items.Count
MyDataGrid.DataSource = LocalDS
MyDataGrid.DataBind()
DataSave(LocalDS)
' Code to respond to the Click event of the DELETE button:
' Desc: Deletes the selected row, updates the DataSet,
' then rebinds
If DataExists() = False Then Exit Sub
If MyDataGrid.SelectedIndex = -1 Then Exit Sub
Dim LocalDS As DataSet = DataRetrieve()
LocalDS.Tables(0).Rows(MyDataGrid.SelectedIndex).Delete()
MyDataGrid.EditItemIndex = -1
MyDataGrid.SelectedIndex = -1
MyDataGrid.DataSource = LocalDS
MyDataGrid.DataBind()
DataSave(LocalDS)
' Code to respond to the Click event of the EDIT button:
' Desc: Makes the selected row editable, then rebinds
If DataExists() = False Then Exit Sub
If MyDataGrid.SelectedIndex = -1 Then Exit Sub
Dim LocalDS As DataSet = DataRetrieve()
MyDataGrid.DataSource = LocalDS
MyDataGrid.EditItemIndex = MyDataGrid.SelectedIndex
MyDataGrid.DataBind()
' Code to respond to the Click event of the OK button:
' Desc: Cycles through the TextBox controls used during a
' standard edit, puts the values back in the DataSet,
' then rebinds. Add error handling as appropriate.
' NOTE: This code relies on the first column being a
' selection (>) button (it starts counting the cells
' from position 1, not 0). If you remove that button,
' you may have to change this code.
If DataExists() = False Then Exit Sub
If MyDataGrid.EditItemIndex = -1 Then Exit Sub
Dim intCount As Integer
Dim LocalDS As DataSet = DataRetrieve()
With MyDataGrid
For intCount = 1 To .Items(.EditItemIndex).Cells.Count
If intCount = .Items(.EditItemIndex).Cells.Count _
Then Exit For
' Check that a control exists in this position
If .Items(.EditItemIndex).Cells(intCount).Controls. _
Count Then
' Check for a standard TextBox
If TypeOf (.Items(.EditItemIndex)._
Cells(intCount). _
Controls(0)) _
Is TextBox Then
If CType(.Items(.EditItemIndex). _
Cells(intCount).Controls(0), _
TextBox).Text = "" Then
' Insert a null, if no data
LocalDS.Tables(0).Rows(.EditItemIndex)._
Item( _
intCount - 1) = System.DBNull.Value
Else
LocalDS.Tables(0).Rows(.EditItemIndex). _
Item( _
intCount - 1) = CType(.Items( _
.EditItemIndex).Cells(intCount). _
Controls(0), _
TextBox).Text
End If
End If
End If
Next
.SelectedIndex = -1
.EditItemIndex = -1
DataSave(LocalDS)
.DataSource = LocalDS
.DataBind()
End With
' Code to respond to the Click event of the CANCEL button:
' Desc: Used to cancel an edit. Deselects an selected rows and
' exists the edit mode, then rebinds.
If DataExists() = False Then Exit Sub
MyDataGrid.SelectedIndex = -1
MyDataGrid.EditItemIndex = -1
MyDataGrid.DataSource = DataRetrieve()
MyDataGrid.DataBind()
' Code to respond to the Click event of the UPDATE button:
' Desc: Updates the underlying database, then rebinds.
' Add error handling code as appropriate.
If DataExists() = False Then Exit Sub
MyDataAdapter.Update(DataRetrieve)
MyDataGrid.DataSource = DataRetrieve()
MyDataGrid.DataBind()
Top Tip: Although you may be working with a DataGrid here, don't be afraid of working directly with your data through the DataSet object. In the Essentials, we looked at sample code to do this—and you can easily mix that code in with the above templates for a more personalized, powerful solution.

Click here for a larger image.
Little Known Technique for Confirming Web Deletes
MyDeleteButton.Attributes("onclick") = _
"return confirm('Are you sure you wish to _
delete this record?');"
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

Selecting Multiple Web Form Grid Items, Hotmail-Style
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

Click Anywhere and Select, with a Web Grid
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

Next Week
About the Author