Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do perform a delete on listview that's not bound by a control

I have a ListView and I have set up my Delete Link button. When I clicked Delete however I get "The ListView 'ListView' raised event ItemDeleting which wasn't handled." So I decided to try and implement the DeleteLinkButton_Click() and ListView_ItemDeleted() ... however I can't figure out how to identify which row I've selected for my DeleteLinkButton_Click().

I didn't bind my source through a control instead I used the following method.

ListView.DataSource = myObject.RetreiveInfo()
ListView.DataBind()

I figure if I can identify my row I can access the values of the labels there and pass them to a stored procedure and perform my DELETE.

Can anyone help? If I need to provide more code just let me know!

Edit:

CodeBehind

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   If Not IsPostBack Then
      UpdateDisplay()
   End If
End Sub

Protected Sub UpdateDistplay()
    ListView.DataSource = myObject.RetrieveInfo()
    ListView.DataBind()
End Sub

ASPX Page

<ItemTemplate>
  <tr>
      <td>
         <asp:LinkButton ID="DeleteLinkButton" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Delete this Info?')" runat="server"></asp:LinkButton>
      </td>
      <td>
         <asp:Label ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
      </td>
  </tr>
</ItemTemplate>
like image 988
daveomcd Avatar asked Dec 09 '11 16:12

daveomcd


2 Answers

In page design put this delete button in .

<asp:LinkButton runat="server" ID="lbtnDelete" CommandArgument='<%#DataBinder.Eval(Container,"DataItem.ID") %>'
                                OnClick="lbtnDelete_Click" CssClass="deleteButton" OnClientClick="return confirm('Are you sure?');">
                                    <img src="resources/images/icons/cross.png" alt="Delete" /></asp:LinkButton>

On page behind write this code

protected void lbtnDelete_Click(object sender, EventArgs e)
    {
        int ID = Convert.ToInt32(((LinkButton)sender).CommandArgument);
        Tbl_Mode Tbl_Mode = DataClassesDataContext.Tbl_Modes.Single(p => p.ID == ID);
        DataClassesDataContext.Tbl_Modes.DeleteOnSubmit(Tbl_Mode);
        DataClassesDataContext.SubmitChanges();
        divDelete.Visible = true;
        Bind();
    }
like image 57
Ashfaq Shaikh Avatar answered Sep 28 '22 04:09

Ashfaq Shaikh


You can find out the index of the item being deleted in the ItemDeleting event, so handling that first might help you out a bit more. It looks like this:

Protected Sub myListView_OnItemDeleting(ByVal sender As Object, ByVal e As ListViewDeleteEventArgs)
    Dim deletedIndex As Integer
    deletedIndex = e.ItemIndex
End Sub

At this point, you now have the index of the item whose delete button was clicked, so you can look up the label and perform the DELETE (as you mentioned in your question).

Good luck!

like image 30
Josh Darnell Avatar answered Sep 28 '22 06:09

Josh Darnell