一般在 GridView 的 Command 的删除按钮是沒有提示的确认信息。以下的范例就是要为GridView增加删除时的确认提示信息。
页面文件:*.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Flag,ID"
DataSourceID="SqlDataSource1" EmptyDataText="沒有记录。">
<Columns>
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="FaxNo" HeaderText="FaxNo" SortExpression="FaxNo" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
</Columns>
</asp:GridView>
DataSourceID="SqlDataSource1" EmptyDataText="沒有记录。">
<Columns>
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="FaxNo" HeaderText="FaxNo" SortExpression="FaxNo" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
</Columns>
</asp:GridView>
代码:*.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
Function FindCommandButton(ByVal Control As Control, ByVal CommandName As String) As Button
Dim oChildCtrl As Control
For Each oChildCtrl In Control.Controls
If TypeOf (oChildCtrl) Is Button Then
If String.Compare(CType(oChildCtrl, Button).CommandName, CommandName, True) = 0 Then
Return oChildCtrl
End If
End If
Next
Return Nothing
End Function
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim oButton As Button
If e.Row.RowType = DataControlRowType.DataRow Then
'找到该行的第一个Cell中的删除按钮,即 CommandField 中的删除按钮
oButton = FindCommandButton(e.Row.Cells(0), "Delete")
oButton.OnClientClick = "if (confirm('确定要删除该行吗?')==false) {return false;}"
End If
End Sub
End Class
Inherits System.Web.UI.Page
Function FindCommandButton(ByVal Control As Control, ByVal CommandName As String) As Button
Dim oChildCtrl As Control
For Each oChildCtrl In Control.Controls
If TypeOf (oChildCtrl) Is Button Then
If String.Compare(CType(oChildCtrl, Button).CommandName, CommandName, True) = 0 Then
Return oChildCtrl
End If
End If
Next
Return Nothing
End Function
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim oButton As Button
If e.Row.RowType = DataControlRowType.DataRow Then
'找到该行的第一个Cell中的删除按钮,即 CommandField 中的删除按钮
oButton = FindCommandButton(e.Row.Cells(0), "Delete")
oButton.OnClientClick = "if (confirm('确定要删除该行吗?')==false) {return false;}"
End If
End Sub
End Class