1: <%@ Import Namespace="System.Data" %>
2: <%@ Import Namespace="System.Data.SQLClient" %>
3: <%@ Page Language="VB" %>
4:
5: <script language="VB" Runat="server">
6: Dim ConnString as String
7: Sub Page_Load(Source as Object, E as EventArgs)
8: ConnString=ConfigurationManager.ConnectionStrings("YourNorthwindString").ConnectionString
9: if not page.IsPostBack then
10: BindGrid
11: end if
12: End Sub
13:
14: Sub DeleteRecord(intProd as Integer)
15: Dim Conn As New SqlConnection(ConnString)
16: Dim cmd As New SqlCommand("Delete from Products where productID=@ProductID", Conn)
17: cmd.Parameters.Add(New SqlParameter("@ProductID", intProd))
18: Conn.Open()
19: cmd.ExecuteNonQuery()
20: Conn.Close
21: End Sub
22:
23:
24: Sub DoDelete()
25: Dim i As Integer
26: For i = 0 To gvProducts.Rows.Count - 1
27: Dim dgItem As GridViewRow = gvProducts.Rows(i)
28: Dim lblid As Label = CType(dgItem.FindControl("lblid"), Label)
29: Dim cb As CheckBox = CType(dgItem.FindControl("chk1"), CheckBox)
30: If cb.Checked Then
31: DeleteRecord(CInt(lblid.Text))
32: End If
33: Next i
34: BindGrid()
35: End Sub
36:
37: Sub BindGrid()
38: Dim Conn As New SqlConnection(ConnString)
39: Dim dr as SqlDataReader
40: Dim mySQL as String
41: mySQL="SELECT Top 10 [ProductID], [ProductName], [QuantityPerUnit], " & _
42: "[UnitPrice] FROM [Products]"
43: Dim cmd As New SqlCommand(mySQL, Conn)
44: Conn.Open()
45: dr=cmd.ExecuteReader
46: gvProducts.DataSource=dr
47: gvProducts.DataBind
48: Conn.Close
49: End Sub
50:
51: Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
52: DoDelete()
53: End Sub
54: </script>
55:
56: <head runat="server">
57: <title>Mass Deletes Using a Gridview with Checkboxes</title>
58: </head>
59: <body>
60: <form id="form1" runat="server">
61: <asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False"
62: DataKeyNames="ProductID">
63: <Columns>
64: <asp:TemplateField HeaderText="Mark for Delete">
65: <ItemTemplate>
66: <asp:CheckBox ID="chk1" runat="server" Checked="False" />
67: <asp:Label ID="lblID" Visible="True" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
68: </ItemTemplate>
69: </asp:TemplateField>
70:
71: <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
72: <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit" />
73: <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" HtmlEncode="False" DataFormatString="{0:C}" SortExpression="UnitPrice" />
74: </Columns>
75: </asp:GridView>
76: <asp:Button ID="btnDelete" runat="server" Text="Delete Checked" OnClick="btnDelete_Click" />
77: </form>
78: </body>
79: </html>
80: