给 DataGrid 的删除信息添加提示
一、在 DataGrid 之外用一个按钮来删除
只要给这个按钮添加操作提示,在 Page_Load 里面添加如下代码就可以:
btnDelete.Attributes.Add("onclick", "return confirm('你确认要删除所选定的信息吗?')")
然后在 btnDelete_Click 中写删除代码
二、在 DataGrid 行记录里面点击删除按钮的提示
如果添加 DataGrid 中的 “删除按钮列”好像不好作提示,这里不知有何良方。
如果添加一个模板列则更上面的方法有点像。
1、在 DataGrid 中添加模板列
<asp:TemplateColumn HeaderText="删除">
<ItemTemplate>
<asp:LinkButton id="btnItemDelete" runat="server">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id="btnItemDelete" runat="server">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
2、在 DataGrid1_ItemDataBound 中邦定删除提示提示
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgdDinggou.ItemDataBound
'添加删除提示
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim btnItemDelete As LinkButton = DirectCast(e.Item.FindControl("btnItemDelete"), LinkButton)
btnItemDelete.Attributes.Add("onclick", "return confirm('您真要删除此行吗?')")
End If
End Sub
'添加删除提示
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim btnItemDelete As LinkButton = DirectCast(e.Item.FindControl("btnItemDelete"), LinkButton)
btnItemDelete.Attributes.Add("onclick", "return confirm('您真要删除此行吗?')")
End If
End Sub
3、在 DataGrid1_ItemCommand 中删除记录
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgdDinggou.ItemCommand
'删除记录
If TypeOf e.CommandSource Is LinkButton AndAlso e.CommandSource.id = "btnItemDelete" Then
Dim delectCommand As New SqlCommand("DELETE FROM tsdgk WHERE kzh=@kzh", _connection)
delectCommand.Parameters.Add("@kzh", DataGrid1.DataKeys(e.Item.ItemIndex))
_connection.Open()
Dim iCount As Integer = delectCommand.ExecuteNonQuery
_connection.Close()
BindDataGrid()
'DbConfig.ShowMessage(Me, "成功删除 " & iCount.ToString & " 条记录")
End If
End Sub