Results 1 to 3 of 3

Thread: Delete multiple records using checkboxes

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2004
    Posts
    59

    Delete multiple records using checkboxes

    Hi

    Im Trying to make a datagrid using db connection and asp.net(vb.net).

    So far I have got it to display db contents in the grip, allowing user to add new records, delete 1 record at a time and edit 1 record at a time. This is down by having the delete and edit buttons with every row.

    I now want to allow user to check the checkboxes next to each record so multiple records can be deleted at once.

    How do I do this. I think I need to use some sort of loop.

    This is what I have so far:

    VB Code:
    1. <%@ Page Language="VB" runat="server" debug="true" explicit="true" aspcompat=true validateRequest=false%>
    2. <%@ Import Namespace="System.Data" %>
    3. <%@ Import Namespace="System.Data.SqlClient" %>
    4.  
    5. <script language="VB" runat="server">
    6. Dim MyConn As SqlConnection                         'declare connection
    7. Dim MySQL as String 'store sql string
    8. Sub Page_Load(sender as Object, e as EventArgs)     'for page load
    9.     MyConn = New SqlConnection("abcd")              'connects to intranet db
    10.     page_title.text="test"                          'label on top of page
    11.     If Not Page.IsPostBack                          'if not submit
    12.       BindData()                                    'first procedure to carry out
    13.     End If     
    14. End Sub
    15.  
    16. Sub BindData()
    17.     MySQL = "SELECT * FROM abctest"                 'query to connect for abctest table
    18.     MyConn.Open() 'opens db connection
    19.     Dim strCmd as New SQLCommand(MySQL, MyConn)     'use conn and query to open table
    20.    
    21.     Dim SQLDR as SQLDataReader                      ' declare a datareader
    22.     SQLDR = strCmd.ExecuteReader()                  'translates info in open table
    23.     MyDataGrid.DataSource = SQLDR                   'assign info to datagrid
    24.     MyDataGrid.DataBind() ' loads data
    25.     MyConn.Close() ' closes conn
    26. End Sub  
    27.  
    28. Sub MyDataGrid_Edit(sender As Object, e As DataGridCommandEventArgs) ' highlights the line you want to edit
    29.     MyDataGrid.ShowFooter = False 'hides footer
    30.     MyDataGrid.EditItemIndex = e.Item.ItemIndex     ' display edititemtemplate
    31.     BindData() 'rebind data
    32. End Sub
    33.  
    34. Sub MyDataGrid_Cancel(sender As Object, e As DataGridCommandEventArgs) ' highlights in reverse
    35.     MyDataGrid.ShowFooter = True                    'shows footer
    36.     MyDataGrid.EditItemIndex = -1                   'hides the edititemtemplate
    37.     BindData()                                      'rebinds
    38. End Sub
    39.  
    40. Sub MyDataGrid_ItemDataBound(sender as Object, e as DataGridItemEventArgs) '
    41. If e.Item.ItemType <> ListItemType.Header AND e.Item.ItemType <> ListItemType.Footer then ' if not header or footer, then its delete
    42.     Dim deleteButton as Button = e.Item.FindControl("Delete")   'button variable which is assigned values of delete button
    43.     deleteButton.Attributes.Add("onclick","return confirm('Are you sure you want to delete everything linked to this course?');")    
    44. End If
    45. End Sub
    46.  
    47. Sub MyDataGrid_Delete(sender As Object, e As DataGridCommandEventArgs) '
    48.     MySQL = "DELETE from abctest where id = @Id"    'delete string
    49.     Dim DelCmd as New SQLCommand(MySQL, MyConn)     'executes connection string and delete query
    50.    
    51.     DelCmd.Parameters.Add(New SqlParameter("@Id", MyDataGrid.DataKeys(e.Item.ItemIndex))) '
    52.  
    53.     MyConn.Open()
    54.     DelCmd.ExecuteNonQuery()
    55.     MyConn.Close()
    56.     BindData()
    57. End Sub
    58.  
    59. Sub MyDataGrid_Update(sender As Object, e As DataGridCommandEventArgs)
    60.     MySQL = "UPDATE [abctest] SET [name] = @name, [phone] = @phone WHERE [id] = @Id"
    61.     Dim UpCmd as New SQLCommand(MySQL, MyConn)
    62.  
    63.     Dim Up_name As TextBox
    64.     Dim Up_phone As TextBox
    65.    
    66.     Up_name = e.Item.FindControl("name")
    67.     Up_phone = e.Item.FindControl("phone")
    68.        
    69.     UpCmd.Parameters.Add(New SqlParameter("@Id", MyDataGrid.DataKeys(e.Item.ItemIndex)))
    70.     UpCmd.Parameters.Add(New SQLParameter("@name", Up_name.Text ))
    71.     UpCmd.Parameters.Add(New SQLParameter("@phone", Up_phone.Text))
    72.    
    73.     MyConn.Open()
    74.     UpCmd.ExecuteNonQuery()
    75.     MyDataGrid.ShowFooter = True
    76.     MyDataGrid.EditItemIndex = -1
    77.     MyConn.close()
    78.     BindData()
    79. End Sub
    80.  
    81. Sub MyDataGrid_Add(sender as Object, e as DataGridCommandEventArgs)
    82. If e.CommandName = "Insert" Then
    83.     MySQL = "INSERT INTO abctest (name,phone) VALUES (@name,@phone)"
    84.     Dim InCmd as New SQLCommand(MySQL, MyConn)
    85.    
    86.     Dim In_name As TextBox
    87.     Dim In_phone As TextBox
    88.    
    89.     In_name = e.Item.FindControl("add_name")
    90.     In_phone = e.Item.FindControl("add_phone")
    91.  
    92.     InCmd.Parameters.Add(New SQLParameter("@name", In_name.text))
    93.     InCmd.Parameters.Add(New SQLParameter("@phone", In_phone.text))
    94.  
    95.     MyConn.Open()
    96.     InCmd.ExecuteNonQuery()
    97.     MyConn.Close()
    98.     BindData()
    99. End if
    100. End Sub
    101.  
    102. Sub post_form(sender As Object, e As EventArgs)
    103. page_title.text = ""
    104. end sub
    105. </script>
    106. <form runat="server">
    107.  
    108. <asp:Label id="page_title" runat="server"
    109. Font-Size="11"
    110. Font-Name="Arial"
    111. Font-Bold="true"
    112. ForeColor="#00436c"
    113. />
    114. <br />
    115. <asp:DataGrid id="MyDataGrid" runat="server"
    116.     width="650"
    117.     GridLines="Horizontal"
    118.     BorderColor="#CCCCCC"
    119.     CellPadding="4"
    120.     CellSpacing="0"
    121.     BorderWidth="1"
    122.     Font-Name="Arial"
    123.     Font-Size="8"    
    124.     AutoGenerateColumns="False"
    125.     OnEditCommand="MyDataGrid_Edit"
    126.     OnUpdateCommand="MyDataGrid_Update"
    127.     OnCancelCommand="MyDataGrid_Cancel"
    128.     OnDeleteCommand="MyDataGrid_Delete"
    129.     OnItemDataBound="MyDataGrid_ItemDataBound"
    130.     DataKeyField="id"
    131.     EditItemStyle-BackColor="#eeeeee"
    132.     OnItemCommand="MyDataGrid_Add"
    133.     ShowFooter="True"    
    134. >
    135. <headerstyle Font-Size="8"
    136.     Font-Name="Arial"
    137.     Font-Bold="true"
    138.     BackColor="#CCCCCC"
    139.     VerticalAlign="top" />
    140.  
    141. <footerstyle Font-Size="8"
    142.     Font-Name="Arial"
    143.     BackColor="#FFFFFF"
    144.     VerticalAlign="top" />
    145.  
    146. <itemstyle Font-Size="8"
    147.     Font-Name="Arial"
    148.     VerticalAlign="top"
    149.     BackColor="#FFFFFF" />
    150.  
    151. <EditItemStyle Font-Size="8"
    152.     Font-Name="Arial"
    153.     VerticalAlign="top"
    154.     BackColor="#FFFFFF" /> 
    155.     <Columns>
    156.         <asp:TemplateColumn visible="false">
    157.             <FooterTemplate>
    158.             </FooterTemplate>
    159.             <ItemTemplate>
    160.                 <%# Container.DataItem("id") %>
    161.             </ItemTemplate>
    162.             <EditItemTemplate>
    163.                 <%# Container.DataItem("id") %>
    164.             </EditItemTemplate>        
    165.         </asp:TemplateColumn>
    166.      
    167.         <asp:TemplateColumn HeaderText="Name">
    168.             <FooterTemplate>
    169.                 <asp:TextBox ID="add_name"
    170.                     Font-Size="8"
    171.                     Font-Name="Arial"                
    172.                     Columns="40"
    173.                     Runat="Server" />
    174.             </FooterTemplate>
    175.             <ItemTemplate>
    176.                 <%# Container.DataItem("name") %>
    177.             </ItemTemplate>
    178.             <EditItemTemplate>
    179.                 <asp:TextBox id="name" Text='<%# Container.DataItem("name") %>'
    180.                     Font-Size="8"
    181.                     Font-Name="Arial"              
    182.                     Columns="40"
    183.                     Runat="Server" />
    184.             </EditItemTemplate>
    185.         </asp:TemplateColumn>
    186.  
    187.         <asp:TemplateColumn HeaderText="Phone">
    188.             <FooterTemplate>
    189.                 <asp:TextBox ID="add_phone"
    190.                     Font-Size="8"
    191.                     Font-Name="Arial"                
    192.                     Columns="40"
    193.                     Runat="Server" />
    194.             </FooterTemplate>
    195.             <ItemTemplate>
    196.                 <%# Container.DataItem("phone") %>
    197.             </ItemTemplate>
    198.             <EditItemTemplate>
    199.                 <asp:TextBox id="phone" Text='<%# Container.DataItem("phone") %>'
    200.                     Font-Size="8"
    201.                     Font-Name="Arial"              
    202.                     Columns="40"
    203.                     Runat="Server" />
    204.             </EditItemTemplate>
    205.         </asp:TemplateColumn>
    206.  
    207.         <asp:EditCommandColumn EditText="Edit Info" ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" />    
    208.  
    209.         <asp:TemplateColumn HeaderText="Delete">
    210.             <FooterTemplate>
    211.                 <asp:Button CommandName="Insert" Text="Add" Runat="server" width="55"/>
    212.             </FooterTemplate>
    213.             <ItemTemplate>
    214.                 <asp:Button CommandName="Delete" id="Delete" Text="Delete" Runat="server" />
    215.             </ItemTemplate>
    216.         </asp:TemplateColumn>          
    217.        
    218.         <asp:TemplateColumn HeaderText="Delete All">
    219.             <FooterTemplate>
    220.             </FooterTemplate>
    221.             <ItemTemplate>
    222.                 <asp:CheckBox id="CheckBox1" value='<%# Container.DataItem("id") %>' runat="server" />
    223.             </ItemTemplate>
    224.             <EditItemTemplate>
    225.             </EditItemTemplate>
    226.         </asp:TemplateColumn>
    227.                      
    228.     </Columns>        
    229. </asp:DataGrid>
    230. <table Border="0" cellspacing="0" cellpadding="5" width="600"> 
    231.     <tr>
    232.         <td align="center">
    233.             <asp:Button ID="Submit" text="Delete Checked"
    234.                 OnClick="post_form"
    235.                 Runat="server"             
    236.                 Font-Size="8"
    237.                 Font-Name="Arial"
    238.                 width="100"
    239.                 />
    240.         </td>
    241.     </tr>                          
    242. </table>
    243. </form>

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    What you can do is create a templated columns with a checkbox and when the user clicks the 'delete' button, simply iterate through the grid's rows collection. As you iterate through, do a FindControl on the checkbox and check if it is checked. Hope this makes sense. I do this on one of my pages if you need an example

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2004
    Posts
    59
    Thanks

    If you do have sample code for this, can you please tell me where to find it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width