In order for the user to edit a row in the ListView, they
should be able to change the corresponding row in the ListView to an editable
one. This can be done by adding the following to the ItemTemplate template to
the ListView control.
Listing 9
<asp:LinkButton ID="lnkEdit" CommandName="Edit" runat="server" Text="Edit" />
The command name should be set to “Edit”. That is the most
important aspect for the edit.
The second step for editing a ListView is to put the
ListView in the edit mode. When the user clicks on any of the Edit Link, the
corresponding row in the ListView should be changed to an editable row. This
can be achieved using the following code snippet:
Listing 10
ListView1.EditIndex = e.NewEditIndex;
e is an instance of ListViewEditEventArgs.
Once the ListView is in the edit mode, we have two
possibilities. The first being: Saving the edited data back to the database and
the second is to cancel the edit operation. Canceling the edit operation can be
done using the following code snippet:
Listing 11
ListView1.EditIndex = -1;
When the user clicks the Update link after editing the
needed data, the event ItemUpdating will be fired. We could invoke a method for
this event and within the method the modified data can be retrieved and update
the database using a stored procedure or an inline SQL statement. Just like the
insert operation, we could use the FindControl method to retrieve the modified
values. Another key aspect for any edit operation is that we should know which
row in the database should be updated. For this purpose, we could store the
primary key for each row in the ListView inside a hidden label control or even
a hidden server control.
Editing a DropDownList control within the edit mode of
ListView
Editing a textbox control is very simple. But if you have a
dropdown listbox, then pre-setting the old value for the dropdown might need
some extra coding. There could be many ways you can pre-set the old value for
the DropDownList control within the edit mode. One of the ways is as follows:
When the user clicks the Edit link for any row, get the
value for the DropDownList control and store it in a global variable.
The event, ItemEditing will be fired when the edit link is
clicked. In the ItemTemplate template for the ListView, the value for the
DropDownList control is displayed as a label.
All we have to do is get the value of the label server
control.
Using this label, we could change the index for the
DropDownList control. The following code retrieves the old value for the
DropDownList control (which is stored in a Label control)
Listing 12
ListView1.EditIndex = e.NewEditIndex;
// Get the Value for State
Label lblTemp = (Label)ListView1.Items[e.NewEditIndex].FindControl("lblState");
strCurrentState = lblTemp.Text;
Now using the string, strCurrentState we could change the
index of the DropDownList control within the ItemDataBound event. The following
method will be invoked during an ItemDataBound event occurs.
Listing 13
protected void DataBoundList(Object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
// Get a handle to the ddlState DropDownList control
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlState");
// Make sure we have the handle !
if (ddl != null)
{
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText
(strCurrentState));
}
}
}
Figure 3: ListView Edit Mode