posted 3/10/2009 by Sumit Arora
Many times we face a situation where we want to delete multiple records from the Repeater.To delete multiple records from the repeater on button can be possible but to do so we need to add java script for checking/Unchecking multiple records.You need to add a javascript at page load for selecting multiple checkboxes together inside the repeater data control.Below is the way to do it.We delete multiple records on button OnCommand event by checking whether the checbox inside the repeater is checked or not.If it is checked then that record would be deleted along with all others records which are checked as well.
In the .cs side the code is like this
protected void btnDelete_Command(object sender, CommandEventArgs e) { if (e.CommandName.Equals("Delete")) { foreach (RepeaterItem ri in rptTest.Items) { HtmlInputCheckBox chkId = (HtmlInputCheckBox)ri.FindControl("chkId"); Literal litTestID = (Literal)ri.FindControl("litTestID "); if (chkId != null) { if (chkId.Checked) { TestCollection tests= new TestCollection ();//Collection Class Test test = new Test (); test .TestID = Convert.ToInt32(litTestID .Text.Trim()); tests.Delete(test ); } } } PopulateAllTest(); } }
In .aspx side,the XML is like this:
<asp:Button ID="btnDelete" runat="server" Text="Delete " CommandName="Delete" OnCommand="btnDelete_Command" />
The Repeater data control is like this:
<asp:Repeater ID="rptTest" runat="server" DataMember="TestID"> <HeaderTemplate> <tr> <th><input type="checkbox" id="checkAll" onclick="CheckAll(this);" runat="server" name="checkAll"></th> <th>TestID</th> <th>Test Name</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td ><input type="checkbox" runat="server" id="chkId" onclick="CheckChanged();" checked='false' name="TestID" /></td> <td><asp:Literal ID="litTestID" runat="server" Text=' <%#Eval("TestID")%>'></asp:Literal></td> <td > <b><%#Eval("TestName")%></b> </td> </tr> </ItemTemplate> </asp:Repeater>
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18