Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

How to delete an item using a checkbox in the datalist using asp.net

Hello........
Can Help Me...

How to delete an item using a checkbox in the datalist using asp.net.................

Also..... there is a button in the datalist....
then... when you have select the check in the checkbox in the datalist.....
you click on the button in the datalist... that will delete... the item... after... you have check the item... that you want... to delete by checking at the checkboxes............


Also,.. can provide me with some of the coding....,,, so that

Maybe...you can also... give me the delete statement for deleting the records in the database.

Thanks aLot......

0
fen2009
Asked:
fen2009
1 Solution
 
arif_eqbalCommented:
Why are you using both a checkbox as well as a Delete button.

In case you want to allow the user to select multiple records and then delete all selected records together then use the check box
The code in this case will be
(on the Command button Click)

Private Sub DeleteAll_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles DeleteAll.Click
   Dim isDeleted As Boolean
   Dim anItem As DataListItem

   ' Check each box and see if the item should be deleted.
   For Each anItem In DataList1.Items
      If CType(anItem.FindControl("Delete"), CheckBox).Checked Then
            Dim aTable As DataTable
            aTable = CType(DataList1.DataSource, DataTable)
            aTable.Rows(anItem.ItemIndex).Delete()
      End If
   Next
   DataList1.DataBind()
End Sub


If you want the user to be able to delete individually then use the Delete button for each row.
The code in this case will be

(On the Delete Button Handler)

Private Sub DataList1_DeleteCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _
Handles DataList1.DeleteCommand
   ' Code to delete the item from the data source.
   Dim aTable As DataTable
   aTable = CType(DataList1.DataSource, DataTable)
   aTable.Rows(e.Item.ItemIndex).Delete()
   ' Bind the data after item is deleted.
   DataList1.DataBind()
End Sub

There's another approach also wherein you would use a Delete SQL Query and then recreate the datalist. I think that you can do based on the above code, in case you want to use that approach.

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now