BindGrid();
// Reload the details view
BindDetails();
}
This code is pretty straightforward. It starts by reading the value of the DataKey
of the DetailsView object. As we saw earlier, the DetailsView, like the GridView,
is able to store the ID of the record (or records) it’s displaying. You’ll remember
that we made the DetailsView object aware of the EmployeeID data key when
we bound the DetailsView to its data source in the BindDetails method. We
read this information in the ItemUpdating event handler, like so:
Visual Basic File: AddressBook.aspx.vb (excerpt)
' Read the employee from the DetailsView object
Dim employeeId As Integer = employeeDetails.DataKey.Value
C# File: AddressBook.aspx.cs (excerpt)
// Read the employee from the DetailsView object
int employeeId = (int) employeeDetails.DataKey.Value;
The next step is to find the TextBox objects that contain the updated data. We
do this using the FindControl method, as we’ve seen previously. After we obtain
the control references, we obtain the string values that we’re interested in simply
by reading their Text properties, as is shown in the following code snippets:
Visual Basic File: AddressBook.aspx.vb (excerpt)
' Find the TextBox controls with updated data
Dim newAddressTextBox As TextBox = _
employeeDetails.FindControl("editAddressTextBox")
Dim newCityTextBox As TextBox = _
employeeDetails.FindControl("editCityTextBox")
' Extract the updated data from the TextBoxes
Dim newAddress As String = newAddressTextBox.Text
Dim newCity As String = newCityTextBox.Text
C# File: AddressBook.aspx.cs (excerpt)
// Find the TextBox controls with updated data
TextBox newAddressTextBox =
(TextBox)employeeDetails.FindControl("editAddressTextBox");
TextBox newCityTextBox =
(TextBox)employeeDetails.FindControl("editCityTextBox");
// Extract the updated data from the TextBoxes
string newAddress = newAddressTextBox.Text;
string newCity = newCityTextBox.Text;
466
Chapter 11: Managing Content Using Grid View and Details View