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 didn’t handle the ModeChanging event. The
DetailsView control doesn’t know how to switch itself to edit mode, but fortu-
nately, it’s 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 control’s 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