VB.NET•Manage Editing Events

Listing 3. Use this code to create event handlers to manage the DataList's inline editing events.

Private Sub Page_Load(ByVal sender As _
   System.Object, ByVal e As System.EventArgs) _
   Handles MyBase.Load
   If Not IsPostBack() Then
      BindDataList()
   End If
End Sub

Private Sub pubsDataList_ItemCommand _
   (ByVal sender As System.Object, _
   ByVal e As DataListCommandEventArgs) _
   Handles pubsDataList.ItemCommand
   ' Find out what the control's command name was 
   ' clicked by checking the CommandName property 
   Select Case e.CommandName
      Case "Special"
         ' This is where you put your specialized 
         ' logic for clicking on the record
   End Select
End Sub

Private Sub pubsDataList_EditCommand _
   (ByVal sender As System.Object, _
   ByVal e As DataListCommandEventArgs) _
   Handles pubsDataList.EditCommand
   ' Determine the selected index
   pubsDataList.EditItemIndex = e.Item.ItemIndex
   BindDataList()
End Sub

Private Sub pubsDataList_DeleteCommand _
   (ByVal sender As System.Object, _
   ByVal e As DataListCommandEventArgs) _
   Handles pubsDataList.DeleteCommand
   ' Perform the record deletion here

   ' Now get the new data to populate the DataList
   BindDataList()
End Sub

Private Sub pubsDataList_CancelCommand _
   (ByVal sender As System.Object, _
   ByVal e As DataListCommandEventArgs) _
   Handles pubsDataList.CancelCommand
   ' Reset the edit item index since we've cancelled
   pubsDataList.EditItemIndex = -1
   BindDataList()
End Sub

Private Sub pubsDataList_UpdateCommand _
   (ByVal sender As System.Object, _
   ByVal e As DataListCommandEventArgs) _
   Handles pubsDataList.UpdateCommand
   ' Perform the database update here

   ' Now reset the edit item index to reflect the
   ' update
   pubsDataList.EditItemIndex = -1
   BindDataList()
End Sub

Private Sub BindDataList()
   SqlDataAdapter1.Fill(AuthorsDataSet)
   pubsDataList.DataSource = _
      AuthorsDataSet.Tables("authors").DefaultView
   pubsDataList.DataBind()
End Sub