C# File: EmployeeDirectory.aspx.cs (excerpt)
// Add content to the Literal control
li.Text = "Employee ID: <strong>" + e.CommandArgument +
"</strong><br />";
Disabling View State
If you take a look at the definition of the extraDetailsLiteral control
in EmployeeDirectory.aspx, youll see that we set its EnableViewState
property to False:
File: EmployeeDirectory.aspx (excerpt)
<asp:Literal ID="extraDetailsLiteral" runat="server"
EnableViewState="false" />
When this property is False, its contents arent persisted during postback
events. In our case, once the visitor clicks another View more details button,
all the instances of that Literal control lose their values. This way, at any
given moment, no more than one employees ID will be displayed. If you
change EnableViewState to True (the default value), then click the View
more details button, youll see that all employee IDs remain in the form,
as theyre persisted by the view state mechanism.
Editing DataList Items and Using
Templates
Continuing our journey into the world of DataList, lets learn a little more about
its templates, and see how you can use the EditItemTemplate to edit its contents.
Our goal here is to allow users to change the name or username of any employee.
Start by adding another button to the ItemTemplate of the DataList. This button
will read Edit employee Employee Name and, when clicked, it will cause the item
of which its a part to become editable.
File: EmployeeDirectory.aspx (excerpt)
<ItemTemplate>
<asp:Literal ID="extraDetailsLiteral" runat="server"
EnableViewState="false" />
Name: <strong><%#Eval("Name")%></strong><br />
Username: <strong><%#Eval("Username")%></strong><br />
<asp:LinkButton ID="detailsButton" runat="server"
Text=<%#"View more details about " & Eval("Name")%>
413
Editing DataList Items and Using Templates
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 employees 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 DataLists ItemCommand event
is fired. This time, the CommandName of the new button is EditItem, and when
we discover that this button was clicked, well 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
Handles employeesList.ItemCommand
' Which button was clicked?
If e.CommandName = "MoreDetailsPlease" Then
' Find the Literal control in the DataList item
Dim li As Literal
li = e.Item.FindControl("extraDetailsLiteral")
' Add content to the Literal control
li.Text = "Employee ID: <strong>" & e.CommandArgument & _
"</strong><br />"
ElseIf e.CommandName = "EditItem" Then
' Set the index of the item being edited
employeesList.EditItemIndex = e.Item.ItemIndex
' Bind again the list to update the list
BindList()
End If
End Sub
C# File: EmployeeDirectory.aspx.cs (excerpt)
protected void employeesList_ItemCommand(object source,
DataListCommandEventArgs e)
{
// Which button was clicked?
if (e.CommandName == "MoreDetailsPlease")
{
// Find the Literal control in the DataList item
Literal li;
li = (Literal)e.Item.FindControl("extraDetailsLiteral");
// Add content to the Literal control
li.Text = "Employee ID: <strong>" + e.CommandArgument +
"</strong><br />";
}
else if (e.CommandName == "EditItem")
{
// Set the index of the item being edited
employeesList.EditItemIndex = e.Item.ItemIndex;
// Bind again the list to update the list
BindList();
}
}
415
Editing DataList Items and Using Templates

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.