Wheres RowEdited?
Note that, in the case of the Edit action in the GridView, theres no
RowEdited event. Why not? Well, it wouldnt make much sense to have
oneGridView knows what to do when an editing action is approved to
take place. More specifically, when a row enters edit mode, it is displayed
using the default editing style of the column. The built-in column types (such
as bound columns, check box columns, and so on) have built-in editing
templates, which you can customize by providing custom templates.
Entering Edit Mode
To get a better grasp on all this theory, lets look at another example. Here, well
modify the DetailsView control to let users update employee data. To implement
GridView or DetailsView editing, we can use a CommandField column.
Lets get started. Open AddressBook.aspx in the designer, click the DetailsViews
smart tag, and choose Add New Field. In the Choose a field type drop-down,
select CommandField, and check the Edit/Update checkbox, as shown in Fig-
ure 11.14.
Figure 11.14. Adding the Edit/Update CommandField
If youd prefer to add the new column by hand, do so by adding it in Address-
Book.aspx. Either way, you should end up with the following code:
456
Chapter 11: Managing Content Using Grid View and Details View
File: AddressBook.aspx (excerpt)
<asp:DetailsView id="employeeDetails" runat="server"
AutoGenerateRows="False">
<Fields>
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="State" HeaderText="State" />
<asp:BoundField DataField="Zip" HeaderText="Zip" />
<asp:BoundField DataField="HomePhone"
HeaderText="Home Phone" />
<asp:BoundField DataField="Extension"
HeaderText="Extension" />
<asp:CommandField ShowEditButton="True" />
</Fields>
<HeaderTemplate>
<%#Eval("Name")%>
</HeaderTemplate>
</asp:DetailsView>
The new item will appear in the designer as an Edit link immediately below the
list of columns. If you execute the project and click that Edit link, an exception
will be thrown, telling you that you didnt handle the ModeChanging event. The
DetailsView control doesnt know how to switch itself to edit mode, but fortu-
nately, its extremely easy to write the code yourself.
To have Visual Web Developer generate the ModeChanging event signature for
you, open AddressBook.aspx in Design View, select the DetailsView control,
click the yellow button with the lightning symbol in the Properties window to
bring up the list of the controls events, and double-click on the ModeChanging
entry. This will generate an empty event handler for you, and take you straight
to the function in the code-behind file. Complete the generated code like this:
Visual Basic File: AddressBook.aspx.vb (excerpt)
Protected Sub employeeDetails_ModeChanging( _
ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs) _
Handles employeeDetails.ModeChanging
' Change current mode to the selected one
employeeDetails.ChangeMode(e.NewMode)
' Rebind the grid
BindDetails()
End Sub
457
Entering Edit Mode
C# File: AddressBook.aspx.cs (excerpt)
protected void employeeDetails_ModeChanging(object sender,
DetailsViewModeEventArgs e)
{
// Change current mode to the selected one
employeeDetails.ChangeMode(e.NewMode);
// Rebind the grid
BindDetails();
}
Execute the project and click the Edit button. This will transform the control as
shown in Figure 11.15.
Figure 11.15. The DetailsView in edit mode
In order to understand the code in employeeDetails_ModeChanging, you need
to know about the display modes of the DetailsView control. The DetailsView
458
Chapter 11: Managing Content Using Grid View and Details View

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.