c# - Remove Items From a CheckBox List -
here main form:
<%@ page language="c#" autoeventwireup="true" codefile="checkdelete.aspx.cs" inherits="checkdelete" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org /tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:checkboxlist id="chkitems" runat="server" style="width: 37px"> <asp:listitem value="a"></asp:listitem> <asp:listitem value="b"></asp:listitem> <asp:listitem value="c"></asp:listitem> <asp:listitem value="d"></asp:listitem> <asp:listitem value="e"></asp:listitem> <asp:listitem value="f"></asp:listitem> <asp:listitem value="h"></asp:listitem> </asp:checkboxlist> <asp:button id="button1" runat="server" onclick="button1_click" text="delete" /> <br /> <br /> </form>
code in form:
protected void button1_click(object sender, eventargs e) { (int = 0; < chkitems.items.count; i++) { if (chkitems.items[i].selected == true) { chkitems.items.removeat(i); } } }
in form, want delete items user has checked off. however, if select 3 items, @ least 1 item remain on form after user hits delete. missing?
you'll need make list of items want remove , remove them 1 one.
e.g.
list<listitem> toberemoved = new list<listitem>(); for(int i=0; i<chkitems.items.count; i++){ if(chkitems.items[i].selected == true) toberemoved.add(chkitems.items[i]); } for(int i=0; i<toberemoved.count; i++){ chkitems.items.remove(toberemoved[i]); }
in example, remove items go change index of remaining items you've yet loop through. result "missing" items loop through. imagine that's cause of problem.
Comments
Post a Comment