简体   繁体   中英

How to delete a GridView Row

I have this code until now:

<asp:TemplateField ShowHeader="False" ItemStyle-Width="150px" HeaderStyle-BackColor="#003ca1" HeaderStyle-ForeColor="white">
    <ItemTemplate>
        <asp:LinkButton ID="linkDelete" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick='return confirm("Diesen Eintrag wirklich löschen?");' Text="Löschen" />
    </ItemTemplate>
</asp:TemplateField>

At this moment, the GridView deletes the row physically. I want to catch this in CodeBehind, cancel the delete-operation and want only set a "IsDeleted"-Flag in the database.

How?

You can use the RowDeleting event and the Cancel property of the GridViewDeleteEventArgs object to do this. Here is what the MSDN page says:

The RowDeleting event is raised when a row's Delete button is clicked, but before the GridView control deletes the row. This enables you to provide an event-handling method that performs a custom routine, such as canceling the delete operation, whenever this event occurs.

A GridViewDeleteEventArgs object is passed to the event-handling method, which enables you to determine the index of the current row and to indicate that the delete operation should be canceled. To cancel the delete operation, set the Cancel property of the GridViewDeleteEventArgs object to true. You can also manipulate the Keys and Values collections, if necessary, before the values are passed to the data source.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM