How to Confirm a Delete in an ASP.NET Datagrid...

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體:   |    |    |  超大  

Date: July 17, 2003
Download the code.
Printer Friendly Version

confirm_delete. This simply pops up a confirmation dialog asking the user if he is sure he wants to delete the record. If OK is clicked, the delete happens. If Cancel is clicked, nothing happens. The second thing to note is that our first BoundColumn is an invisible column containing the ProductID (we are using the Northwind Products table), which is the primary key we will use for the delete. Most importantly, please note that we have added a Template Column in which we have placed an asp:Button (you could use a LinkButton instead if you prefer). We have given it an ID of "btnDelete" and a CommandName of "Delete". The latter is what makes it work with the Datagrid's OnDeleteCommand.

<html>

<head>

<title>ConfirmDelDG</title>

<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">

<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">

<meta name=vs_defaultClientScript content="JavaScript">

<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">

<script language="javascript">

function confirm_delete()

{

if (confirm("Are you sure you want to delete this item?")==true)

return true;

else

return false;

}

</script>

</head>

<body>

<form method="post" runat="server" ID="Form1"><br><br>

<asp:DataGrid id="dtgProducts" runat="server"

CellPadding="6" AutoGenerateColumns="False"

OnDeleteCommand="Delete_Row" BorderColor="#999999"

BorderStyle="None" BorderWidth="1px"

BackColor="White" GridLines="Vertical">

<AlternatingItemStyle BackColor="#DCDCDC" />

<ItemStyle ForeColor="Black" BackColor="#EEEEEE" />

<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084" />

<Columns>

<asp:BoundColumn Visible="False" DataField="ProductID" ReadOnly="True" />

<asp:BoundColumn DataField="ProductName" ReadOnly="True" HeaderText="Name" />

<asp:BoundColumn DataField="UnitPrice" HeaderText="Price" DataFormatString="{0:c}"

ItemStyle-HorizontalAlign="Right" />

<asp:TemplateColumn>

<ItemTemplate>

<asp:Button id="btnDelete" runat="server" Text="Delete" CommandName="Delete" />

</ItemTemplate>

</asp:TemplateColumn>

</Columns>

</asp:DataGrid>

</form>

</body>

</html>

Imports System.Data.SqlClient

Imports System.Configuration

Imports System.Web.UI.WebControls

Public Class ConfirmDelDG

Inherits System.Web.UI.Page

Protected WithEvents dtgProducts As System.Web.UI.WebControls.DataGrid

Private strConnection As String = ConfigurationSettings.AppSettings("NorthwindConnection")

Private strSql As String = "SELECT ProductID, ProductName, UnitPrice " _

& "FROM Products WHERE CategoryID = 1"

Private objConn As SqlConnection

Private Sub Page_Load(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then

BindTheGrid()

End If

End Sub

Private Sub BindTheGrid()

Connect()

Dim adapter As New SqlDataAdapter(strSql, objConn)

Dim ds As New DataSet()

adapter.Fill(ds, "Products")

Disconnect()

dtgProducts.DataSource = ds.Tables("Products")

dtgProducts.DataBind()

End Sub

If objConn Is Nothing Then

objConn = New SqlConnection(strConnection)

End If

If objConn.State = ConnectionState.Closed Then

objConn.Open()

End If

End Sub

Private Sub Disconnect()

objConn.Dispose()

End Sub

ByVal e As DataGridItemEventArgs) Handles dtgProducts.ItemDataBound

Dim btn As Button

If e.Item.ItemType = ListItemType.Item or e.Item.ItemType = ListItemType.AlternatingItem Then

btn = CType(e.Item.Cells(0).FindControl("btnDelete"), Button)

btn.Attributes.Add("onclick", "return confirm_delete();")

End If

End Sub

' Retrieve the ID of the product to be deleted

Dim ProductID As system.Int32 = System.Convert.ToInt32(E.Item.Cells(0).Text)

dtgProducts.EditItemIndex = -1

' Create and load a DataSet

Connect()

Dim adapter As New SqlDataAdapter(strSql, objConn)

Dim ds As New DataSet()

adapter.Fill(ds, "Products")

Disconnect()

' Mark the product as Deleted in the DataSet

Dim tbl As DataTable = ds.Tables("Products")

tbl.PrimaryKey = New DataColumn() _

{ _

tbl.Columns("ProductID") _

}

Dim row As DataRow = tbl.Rows.Find(ProductID)

row.Delete()

' Reconnect the DataSet and delete the row from the database

'-----------------------------------------------------------

' Following section commented out for demonstration purposes

'Dim cb As New SqlCommandBuilder(adapter)

'Connect()

'adapter.Update(ds, "Products")

'Disconnect()

'-----------------------------------------------------------

' Display remaining rows in the DataGrid

dtgProducts.DataSource = ds.Tables("Products")

dtgProducts.DataBind()

End Sub

End Class

You may download the code here.

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航