Solved

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

Posted on 2004-10-03
3
451 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
[X]
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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 264
Cygwin - GNU GPL License 1 57
IDE for Python 5 105
Cannot locate cell 15 42
A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.

734 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