Hello. I have a linkbutton that will delete individual rows from my gridview and update the database, however, I want to be able to make multiple selections via the checkboxes which appear on each row, and click just one button to delete all the checked rows. The following is from my aspx page:
<div style="float: right;"> <asp:Button ID="btnLinkBack" runat="server" Text="Check Backlinks" /> <asp:Button ID="btnApprove" runat="server" Text="Unhide" /> <asp:Button ID="btnDisapprove" runat="server" Text="Hide" /> <asp:Button ID="btnDelete" runat="server" Text="Delete" /> </div> <br /><br /><br /> <asp:GridView ID="gvLink" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="false" DataKeyNames="AdvLinkID" DataSourceID="objDS" OnDataBinding="gv_DataBinding" OnPageIndexChanging="gv_PageIndexChanging" OnRowCommand="gv_RowCommand" OnRowDeleting="gv_RowDeleting" PagerSettings-Mode="NumericFirstLast" PagerSettings-PageButtonCount="10" PagerSettings-Position="TopAndBottom" PagerStyle-HorizontalAlign="right" Visible="false" Width="100%" PageSize="50"> <Columns> <asp:TemplateField HeaderText="Select" ItemStyle-CssClass="GridHeader"> <ItemTemplate> <asp:CheckBox ID="chkSelect" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Link" SortExpression="SiteURL"> <ItemTemplate> <a href='<%#Container.DataItem("SiteURL")%>' target="_blank"> <%#BusinessLogic.WebSite.TrimURL(Container.DataItem("SiteURL"), 40)%> </a> <br /> <span class="SiteTitle"> <%#Container.DataItem("SiteTitle")%> </span> <br /> <span class="SiteDescription"> <%#Container.DataItem("SiteDescription") %> </span> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Reciprocal URL" SortExpression="ReciprocalURL" ItemStyle-Width=""> <ItemTemplate > <a href="<%#Container.DataItem("ReciprocalURL")%>" target="_blank" title='<%#Container.DataItem("ReciprocalURL")%>'> <%#BusinessLogic.WebSite.BreakReciprocalURL(Container.DataItem("AdvLinkID"), 40)%> </a> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Back Link" SortExpression="LinkBack" ItemStyle-HorizontalAlign="center"> <ItemTemplate> <img src="../images/bullets/<%#CBool(Container.DataItem("LinkBack"))%>.gif" alt="link back status" style="text-align:center;" id="linkback"/> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Last Checked" SortExpression="LastChecked"> <ItemTemplate> <asp:Label Text='<%# Eval("LastChecked", "{0:d}") %>' runat="server" ID="lblLastChecked" /><br /> <asp:Button ID="btnEmail" OnClick="EmailWebmaster_Click" runat="server" Text="Email WebMaster" Visible="false" CommandArgument='<%#Container.DataItem("LinkID")%>' Font-Size="XX-Small" /><br /> <asp:Label Text='Emailed on' runat="server" ID="Label1" Visible="false"/> <asp:Label Text='<%# Eval("LastEmailed", "{0:d}") %>' runat="server" ID="lblLastEmailed" Visible="false"/><br /> <asp:Label Text='Check back in 5 days' runat="server" ID="Label2" Visible="false"/> <asp:Label Text='No email available. Contact webmaster through their website.' runat="server" ID="Label3" Visible="false"/> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <div class="Highlight"> <asp:LinkButton ID="lnkHide" runat="server" CommandArgument='<%#Container.DataItem("AdvLinkID")%>' OnClick="HideUnHideLink" Text='<%#GetHideText(cbool(Container.DataItem("Approved")))%>'> </asp:LinkButton> <a href='adv_links_editbyuser.aspx?lid=<%#Container.DataItem("AdvLinkID")%>'>Edit</a> <br /> <asp:LinkButton ID="lnkDelete" runat="server" CommandArgument='<%#Container.DataItem("AdvLinkID")%>' ForeColor="red" OnClick="DeleteLink" OnClientClick="return confirm('Are your sure to delete this link?')" Text="Delete"> </asp:LinkButton> </div> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <div style="float: left;"> </div> <div style="float: right;"> <asp:Button ID="btnLinkBack2" runat="server" Text="Check Backlinks" /> <asp:Button ID="btnApprove2" runat="server" Text="Unhide" /> <asp:Button ID="btnDisApprove2" runat="server" Text="Hide" /> <asp:Button ID="btnDelete2" runat="server" Text="Delete" /> </div> <br /> <br /> </td> </tr> </asp:Panel> </table> <asp:ObjectDataSource ID="objDS" runat="server" DeleteMethod="Delete" SelectMethod="GetbyFieldValue" TypeName="BusinessLogic.AdvanceLink"> <DeleteParameters> <asp:Parameter Name="AdvLinkID" Type="Int32" /> </DeleteParameters> <SelectParameters> <asp:Parameter DefaultValue="WebSiteID" Name="field" Type="String" /> <asp:ControlParameter Name="fieldValue" DefaultValue="0" ControlID="lstAdvanceSites" PropertyName="SelectedValue" Type="String" /> <asp:Parameter DefaultValue="=" Name="criteria" Type="String" /> <asp:Parameter DefaultValue="AdvanceCategoryID>0" Name="whereBy" Type="String" /> <asp:Parameter DefaultValue="LastChecked" ConvertEmptyStringToNull="true" Name="sortBy" Type="String" /> <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="tran" /> </SelectParameters> </asp:ObjectDataSource>1 Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) 2 Dim grid As GridView = CType(sender, GridView) 3 grid.PageIndex = e.NewPageIndex 4 grid.DataBind() 5 End Sub 6 7 Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) 8 9 End Sub 10 Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click 11 Dim gvIDs As String = "" 12 Dim chkBox As Boolean = False 13 'Navigate through each row in the GridView for checkbox items 14 For Each gv As GridViewRow In gvLink.Rows 15 Dim deleteChkBxItem As CheckBox = CType(gv.FindControl("chkSelect"), CheckBox) 16 If deleteChkBxItem.Checked Then 17 chkBox = True 18 gvIDs += CType(gv.FindControl("AdvLinkID"), Label).Text.ToString + "," 19 End If 20 Next 21 Dim cn As New SqlConnection(DataFuncs.GetConnectionString) 22 If chkBox Then 23 Try 24 Dim deleteSQL As String = _ 25 "DELETE from WHERE employeeid IN (" + _ 26 gvIDs.Substring(0, gvIDs.LastIndexOf(",")) + ")" 27 Dim cmd As SqlCommand = New SqlCommand(deleteSQL, cn) 28 cn.Open() 29 cmd.ExecuteNonQuery() 30 gvLink.DataBind() 31 Catch Err As SqlException 32 Response.Write(Err.Message.ToString) 33 Finally 34 cn.Close() 35 End Try 36 End If 37 End Sub 38I had added the above for btnDelete click from some code I found online. Can someone help me complete this? The main id is AdvlinkID which identifies the row in the table.
Many thanks.
Scot King
SEO Software Tool | Search Engine Optimization
"Get your page rank high"
![]() |
0 |
![]() |
Your on the right track for sure. I modified your code slightly to fire a method every time the check box is true, i think i have read your code correctlyProtected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click Dim gvIDs As String = "" Dim chkBox As Boolean = False 'Navigate through each row in the GridView for checkbox items For Each gv As GridViewRow In gvLink.Rows If CType(gv.FindControl("chkSelect"), CheckBox).Checked Then 'Modified to create a method to run on each Checked=true checkbox DeleteItem(CType(gv.FindControl("AdvLinkID"), Label).Text.ToString) End If Next End Sub 'Runs the sql command to delte the employee on each true checkbox, returns a Boolean wether or not complete. 'You could change this to a string to send back an error message if you would prefer. Private Function DeleteItem(ByVal employee As String) As Boolean If String.IsNullOrEmpty(employee) Then Return False End If Dim completed As Boolean = False Dim cn As New SqlConnection(DataFuncs.GetConnectionString) Dim cmd As SqlCommand = New SqlCommand() cmd.Connection = cn cmd.CommandType = CommandType.Text cmd.CommandText = "DELETE from WHERE employeeid =@employeeID" cmd.Parameters.AddWithValue("@employeeID", employee) Try cn.Open() cmd.ExecuteNonQuery() completed = True Catch Err As Exception completed = False Finally cn.Close() End Try Return completed End Function
Please mark post as answer
Obilit.com - Progressive Web Methodologies
My Blog
![]() |
0 |
![]() |
The keyval seems to get the integer I need to pass to the delete routine. It deletes the links from 3 databases.
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click, btnDelete2.Click
Deletelinks()
End Sub
Private Sub Deletelinks()
Dim grid As GridView = Me.gvLink
Dim keyVal As String
For Each itm As GridViewRow In grid.Rows
keyVal = grid.DataKeys(itm.RowIndex).Value
If CType(itm.FindControl("chkSelect"), CheckBox).Checked Then
AdvanceLink.Delete(keyVal, True)
End If
Next
grid.DataBind()
Me.lblMsg.Text = "All selected links deleted"
End Sub
Scot King
SEO Software Tool | Search Engine Optimization
"Get your page rank high"
![]() |
0 |
![]() |