Allora, ripescando un vecchio esempio.
Ho una DataGrid. Ogni record ha un pulsante e un link per la cancellazione del record.
nota che il primo ha un id; il secondo, no.codice:<asp:TemplateColumn HeaderText="Delete?"> <ItemTemplate> <asp:Button runat="server" ID="btnDelete" Text="Delete Row" CommandName="cancella" CommandArgument=""></asp:Button> </ItemTemplate> </asp:TemplateColumn> <asp:ButtonColumn ButtonType="LinkButton" Text="Cancella" CommandName="Delete" HeaderText="Cancella?" />
nell'evento ItemCreated aggiungiamo il javascript
Nota le due tecniche:codice:Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated Select Case e.Item.ItemType Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem Dim btn As Button btn = DirectCast(e.Item.FindControl("btnDelete"), Button) If Not (btn Is Nothing) Then btn.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this row?')") End If Dim cella As TableCell = e.Item.Cells(17) Dim btn1 As LinkButton = DirectCast(cella.Controls(0), LinkButton) If Not (btn1 Is Nothing) Then btn1.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this row?')") End If End Select End Sub
il primo pulsante si individua con FindControl
il secondo si individua dal numero di cella

Rispondi quotando
