Allow users to delete items in the DataList web server control

xiaoxiao2021-03-06  15

MSDN Home> Visual Studio .NET> Visual Basic and Visual C #> Create Applications> Creating Web Applications & Services> ASP.NET Server Controls> Controls for Use on Web Forms> DataList Web Server Controls

Visual Basic and Visual C # Concept

Allow users to delete items in the DataList web server control

You can allow users to delete items in the DataList control in various ways. One way is to include the "Delete" button in the item, and then delete the item immediately when the user clicks it.

Another method is to include a check box in a single item. The user can then select all items to be removed, and then click the separate "Delete" button to delete them. This method is used in a program such as MSN Hotmail.

Allow users to delete a single item

Add a Button or LinkButton web server control in ItemTemplate (and AlternatingItemTemplate if you want to use it). Set the commandName property of this button to Delete (case sensitive). Create an event handler for the DeleteCommand event. For more information, see Creating an Event Handle in a web form page. In this method:

Remove the item from the data source and then rebound the DataList control. The index of the user's clicks can be obtained through the ItemIndex property of the Item object. To get the value of a single control, use the FindControl method for the ITEM event parameter object. Re-bind the control to its data source. If the data source is a DataTable object, the event handler may look like this: 'Visual Basic

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 Deleded.

DataList1.databind ()

End Sub

// c #

Private void DataList1_DeleteCommand (Object Source,

System.Web.ui.WebControls.DatalistCommandEventArgs E)

{

// code to delete the item from the data source.

DataTable Atable = (DATATABLE) DATALIST1.DATASOURCE;

Atable.Rows [E.Item.ItemIndex] .delete ();

// bind The data after the item is deleded.

DataList1.DATABIND ();

}

Allow users to delete multiple items at a time

Add the checkbox web server control in ItemTemplate, add the checkbox web server control and set its ID attribute to "Delete". Make sure the AutopostBack property of the Checkbox control is false. Close the template editing by right-clicking on the DataList control and selecting "End Template Editing" from the menu. Add the Button Web Server Control to the web form page. Set the Text property to "All Remove" and set the ID property to Deletell. This button is not added to one of the DataList template. Create a method for the Click event for the All Delete button. In the method: Each item is extracted in order by the Items collection of the DataList control. Within the item, use the FindControl method of this item to get the checkbox control of step 1 and test its Checked property. If the box is selected, the corresponding item is removed from the data source. Re-bind the DataList control to its data source. The following example shows the event handler of the Deleteall button, which uses the procedure described above into a batch deletion item. 'Visual Basic

Private sub deleteall_click (Byval Sender as Object, _

Byval e as system.eventargs) Handles deleteall.click

Dim isdeleted as boolean

DIM ANITEM AS DATALISTIM

'CHECK Each Box and See If The Item Should Be deled.

For Each Antem in Datalist1.Items

Isdeleted = CType (Antem.FindControl ("delete"), checkbox) .checked

If isdeleted then

'Add code here to delete the item, using, usitm.itemindex.

END IF

NEXT

DataList1.databind ()

End Sub

// c #

Protected void deleteall_click (Object Sender, System.Eventargs E)

{

Bool isdeleted;

// Check Each Box and See if The item shouth be deleted.

Foreach (DatalistItem AnItem in DataList1.Items)

{

Isdeleted =

(Checkbox) Antem.FindControl ("delete")). Checked;

IF (isdeleted)

{

// Add code here to delete the item, using, usagem.Index.

}

}

DataList1.DATABIND ();

}

See

Description DataList Web Server Control | Allow users to edit items in the DataList Web Server Control | Allow users to select items in the DataList Web Server Control | Creating event handles in a web form page | Responding to DataList, Repeater, or DataGrid items

转载请注明原文地址:https://www.9cbs.com/read-44841.html

New Post(0)