Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2004-10-03
3
428 Views
Last Modified: 2008-03-10
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
Comment
Question by:fen2009
3 Comments
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 50 total points
ID: 12214610
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about a problem I had getting the GPS LocationListener working.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question