CommandName="MoreDetailsPlease"
CommandArgument=<%#Eval("EmployeeID")%> /><br />
<asp:LinkButton ID="editButton" runat="server"
Text=<%#"Edit employee " + Eval("Name")%>
CommandName="EditItem"
CommandArgument=<%#Eval("EmployeeID")%> />
</ItemTemplate>
When an Edit employee button is clicked, we will make the item enter edit mode.
When one of the DataList items is in edit mode, the EditItemTemplate template
of the DataList is used to generate the contents of that item. All the other items
are generated by the ItemTemplate, as usual.
Modify EmployeeDirectory.aspx by adding the EditItemTemplate to the
DataList. The EditItemTemplate contains TextBox controls into which the user
can enter the employee’s name and username, and two buttons: Update Item and
Cancel Editing, whose names are self-explanatory.
File: EmployeeDirectory.aspx (excerpt)
<EditItemTemplate>
Name: <asp:TextBox ID="nameTextBox" runat="server"
Text=<%#Eval("Name")%> /><br />
Username: <asp:TextBox ID="usernameTextBox" runat="server"
Text=<%#Eval("Username")%> /><br />
<asp:LinkButton ID="updateButton" runat="server"
Text="Update Item" CommandName="UpdateItem"
CommandArgument=<%#Eval("EmployeeID")%> />
or
<asp:LinkButton ID="cancelButton" runat="server"
Text="Cancel Editing" CommandName="CancelEditing"
CommandArgument=<%#Eval("EmployeeID")%> />
</EditItemTemplate>
Finally, before you can see your new template, we need to handle the Edit employ-
ee button. Again, when that button is clicked, the DataList’s ItemCommand event
is fired. This time, the CommandName of the new button is EditItem, and when
we discover that this button was clicked, we’ll put the item into edit mode. To
put a DataList item into edit mode, we set its EditItemIndex to the index of
the item, then bind the DataList to its data source again to refresh its contents.
Add this code:
Visual Basic File: EmployeeDirectory.aspx.vb (excerpt)
Protected Sub employeesList_ItemCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _
414
Chapter 10: Displaying Content Using Data Lists