开发者

ASP.NET - How to wrap GridView Delete in try catch or stop a row delete

开发者 https://www.devze.com 2022-12-08 10:23 出处:网络
I want to be able to wrap a gridview row delete in a try catch and display a nice error message on the screen or try to stop the delete from happening in certain circumstances.

I want to be able to wrap a gridview row delete in a try catch and display a nice error message on the screen or try to stop the delete from happening in certain circumstances.

At the moment, I am getting foreign key violations in the database. So I either want to stop the delete from happening if there are child records o开发者_Go百科r catch the foreign key exception and display a nice error message to the screen.

Can anyone tell me how to do this?


You can use GridView Row Deleting Event

Here is a sample code for that :

void CustomersGridView_RowDeleting
        (Object sender, GridViewDeleteEventArgs e)
    {
        TableCell cell = CustomersGridView.Rows[e.RowIndex].Cells[2];
        if (cell.Text == "Beaver")
        {
            e.Cancel = true;
            Message.Text = "You cannot delete customer Beaver.";
        }
        else
        {
            Message.Text = "";
        }
    } 


Using the datasource Deleting event might be cleaner as one does not depend on the GUI elements and their possible repositioning that might break the code.

0

精彩评论

暂无评论...
验证码 换一张
取 消