Bonjour,
Je veux supprimer une ligne de mon gridview après confirmation. voilà le principe:
j'ai un gridview avec une colonne contenant le bouton de suppression comme suit

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
<asp:GridView ID="gv_type" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" PageSize="9" Width="100%" DataSourceID="sds_type" ShowHeader="false" DataKeyNames="Id">
        <Columns>
            <asp:BoundField DataField="Valeur" HeaderText="Type" SortExpression="Valeur" ReadOnly="True" ItemStyle-Width="75%"/>
            <asp:ButtonField ButtonType="Image" CommandName="Modifier" ImageUrl="~/Images/bt_vue.png" ItemStyle-CssClass="DataGridModifier" ItemStyle-HorizontalAlign="Center"/>
            <asp:ButtonField ButtonType="Image" CommandName="Supprimer" ImageUrl="~/Images/bt_delete.png" ItemStyle-HorizontalAlign="Center"/>
        </Columns>
    </asp:GridView>                                       
        <asp:SqlDataSource
            ID="sds_type"
            runat="server"
            ConnectionString="<%$ ConnectionStrings:LGHI %>"
            SelectCommand="SELECT * FROM p_type_vehicule">
    </asp:SqlDataSource>
Dans le code behind, pour supprimer une ligne je fais
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Private Sub gv_type_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gv_type.RowCommand
        On Error Resume Next
        Dim Idx As Integer = Convert.ToInt32(e.CommandArgument)
        clef_primaire.Value = gv_type.DataKeys(Idx)("Id")
 
        Select Case e.CommandName
            Case "Modifier"
                Session("Demarrage") = False
                Page.ClientScript.RegisterClientScriptBlock([GetType], "popup depuis le codehind", String.Format("window.open('{0}','_blank','width={1},height={2},top={3},left={4}')", "p_donnee_base_maj.aspx?param1=p_type_vehicule&param2=false&param3=" & clef_primaire.Value, 700, 400, 60, 20), True)
            Case "Supprimer"
                ClientScript.RegisterStartupScript([GetType](), "Suppression", "<script> if (confirm ('Confirmez-vous la suppression?')) window.open('a_page_suppression.aspx?param1=p_type_vehicule&param2=" & clef_primaire.Value & "&param4=Id', '_newtab') </script>")
                ActualiserTypeVehicule()
            Case Else
                ActualiserTypeVehicule()
        End Select
    End Sub
J'appelle donc une webform ["a_page_suppression.aspx?param1=p_type_vehicule&param2=" & clef_primaire.Value & "&param4=Id'] dans laquelle je lance la procédure de suppression. Je referme automatiquement aussitôt la webform de suppression après suppression.
Je trouve cette méthode quelque peu fastidieuse.
N'est-il pas possible de mettre à la place de la webform de suppression une procédure de suppression "SupprimerEnregistrementDansGridView"
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Private Sub SupprimerEnregistrementDansGridView
        Try
            SqlConn.Open()
            Dim myCommand As New SqlCommand
            myCommand.Connection = SqlConn
            myCommand.CommandText = "delete from p_type_vehicule where (Id=@Idx)"
            myCommand.Parameters.Add(New SqlParameter("@Idx", clef_primaire.Value))
 
            myCommand.ExecuteNonQuery()
            SqlConn.Close()
            myCommand = Nothing
        Catch
            ClientScript.RegisterStartupScript([GetType](), "alert", "<script language=JavaScript>alert('< " & Err.Number & " - " & Replace(Err.Description, "'", " ") & " >');</script>")
        End Try
    End Sub
Si je peux avoir un tuyau sur le code javascript dans la gridview. Je plante complètement.