(3/3) ListView events, ItemEditing, ItemCanceling, ItemUpdating

1517
1
SHARE


Edit Record

Same for this Edit Record, Save Record and Cancel button click events.

Wire the events in the code-behind first.

LstItems.ItemEditing += new EventHandler<ListViewItemEventArgs>(LstItems_ItemEditing); LstItems.ItemUpdating += new EventHandler<ListViewItemEventArgs>(LstItems_ItemUpdating); LstItems.ItemCanceling += new EventHandler<ListViewItemEventArgs>(LstItems_ItemCanceling);

Button “Edit” click.

protected void LstItems_ItemEditing(object sender, ListViewItemEventArgs e) { var item = LstItems.Items[e.NewEditIndex]; // Save the currently selected item to OldDnsRecord
// ReBind the ListView }

Button “Save” click.

protected void LstItems_ItemUpdating(object sender, ListViewItemEventArgs e) { if (!Page.IsValid) return; var txtNewHost = e.Item.FindControl<TextBox>("TxtNewHost"); var txtNewPointsTo = e.Item.FindControl<TextBox>("TxtNewPointsTo"); var ddlNewTtl = e.Item.FindControl<DropDownList>("DdlNewTTL"); var host = txtNewHost.Text; var pointsTo = txtNewPointsTo.Text; var ttl = ddlNewTtl.SelectedValue; var result = UpdateExistingRecord(); if (result == Status.Ok) { LstItems.EditIndex = -1; // Rebind the ListView } else { // Do error handling } }

Button “Cancel” click.

protected void LstItems_ItemCanceling(object sender, ListViewItemEventArgs e) { LstItems.EditIndex = -1; // Rebind the ListView }

1 COMMENT

LEAVE A REPLY