Inserire la conferma di cancellazione per gli elementi di un Datagrid
<%@ Page language="VB" Debug="false" AutoEventWireup="false"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<HTML>
<script language="VB" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
if Not Page.IsPostBack Then
Dim dbConn As OleDbConnection
Dim myAdapter As OleDbDataAdapter
Dim sqlCmd As String
Dim strConn As String
Dim oDataSet As New DataSet
strConn = ""
strConn = strConn & "Provider=Microsoft.Jet.OLEDB.4.0;"
strConn = strConn & "Data Source=" & Server.MapPath("database/utenti.mdb") & ";"
strConn = strConn & "Persist Security Info=False"
sqlCmd = ""
sqlCmd = sqlCmd & "SELECT "
sqlCmd = sqlCmd & " nome, "
sqlCmd = sqlCmd & " cognome, "
sqlCmd = sqlCmd & " email "
sqlCmd = sqlCmd & " FROM utenti"
dbConn = New OleDbConnection(strConn)
myAdapter = New OleDbDataAdapter(sqlCmd, dbConn)
myAdapter.Fill(oDataSet, "utenti")
myDatagrid.DataSource = oDataSet.Tables("utenti").DefaultView
myDatagrid.DataBind()
End If
End Sub
Sub myDatagrid_OnItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim c As Button =e.Item.Cells(0).FindControl("btnElimina")
c.Attributes("onClick") = "return(confirm('Confermi?'))"
End If
End Sub
Sub myDatagrid_OnDeleteCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
'qui andebbe inserito il codice per cancellare la riga
lbMessaggio.Text = "CANCELLAZIONE EFFETTUATA"
End Sub
</script>
<body>
<form runat="server">
<asp:datagrid id="myDatagrid" runat="server" OnItemDataBound="myDatagrid_OnItemDataBound" OnDeleteCommand="myDatagrid_OnDeleteCommand">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button id="btnElimina" runat="server" CommandName="Delete" Text="Elimina"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<asp:Label id="lbMessaggio" runat="server"></asp:Label>
</form>
</body>
</HTML>