Solved

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

Posted on 2004-10-03
3
372 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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.
This article will show, step by step, how to integrate R code into a R Sweave document
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now