views:

111

answers:

1

I'm adding items dynamically when a row is selected from GridView.

1. How can i make the items added are selected by default - (solved) 
2. How can i avoid duplicates getting added to list 
3. How can i remove them from list when user un-checks them.

And I want to change checkbox with an image and I'm using css like following but it is not working

.cbxCustom
{
...
}
.cbxCustom tr td checkbox
{
....
}

 <asp:CheckBoxList ID="cbl1" runat="server" AutoPostBack="true"
   OnSelectedIndexChanged="cbl1_OnSelectedIndexChange"     
   CssClass="cbxCustom">                                
 </asp:CheckBoxList>
A: 

Duplicates can be avoided by keeping your items in a List and before adding the item, check to see if lst.Contains(itemName). If it does, don't add it.

Removing them from the list is a little more problematic because it depends on how deeply you want to go into it. I'm assuming you don't want a PostBack for each click of a checkbox. If you want a PostBack, you'll need a way of serializing your list of checkboxes and information into a control in the ViewState. Since you're dynamically adding this list in the code-behind, I'd recommend an invisible Literal whose job is just to hold a serialized string holding your data.

If you DON'T want a PostBack, you'll need to work up a significant amount of JavaScript to modify the InnerHTML of the table that is output by the CheckBoxList. Again, as above, I'd use a Literal or some ViewState managed invisible control to serialize the state. In this case a literal wouldn't work, but a might.

Joel Etherton