How to move listitems(which are marked) in checkboxlist at run time?
Help is appreciated.....

Dani AI

Generated

The server-side approach that posted is a common, reliable way to move selected ListItem objects between ASP.NET list controls and it explains why got the expected result. A few practical caveats and improvements that tend to matter over time:

  • Data binding: if either control is rebound on every Page_Load, moved items will be lost. Bind only when not a postback (or restore the state from a persisted source) so runtime changes survive postbacks.
  • Removal while iterating: removing items from a collection during a forward loop can skip items. Either iterate backward or collect the items-to-move into a temporary list first, then add/remove from the controls.
  • Duplicates and selection state: check the target control for an existing Value before adding to avoid duplicates; clear Selected on moved items so they do not remain selected in both lists.
  • Dynamic controls and ViewState: controls created dynamically must be recreated early (Page_Init) with the same IDs so ViewState and selection survive.

For better UX, perform the move client-side (DOM manipulation or a dual-list JS widget) and submit the updated item list in a hidden field or via AJAX. Note that ASP.NET can change client IDs unless ClientIDMode is controlled, and any server-side dynamic controls still need proper lifecycle handling to accept the client-side changes on postback.

Checklist: guard DataBind with IsPostBack, avoid duplicates, clear selection after move, recreate dynamic controls early, and consider a client-side solution or AJAX for large lists or frequent moves.

Recommended Answers

All 2 Replies

Try out,

....
 for(int i=0;i<ListBox1.Items.Count;)
        {
            if (ListBox1.Items[i].Selected)
            {
                CheckBoxList1.Items.Add(ListBox1.Items[i]);
                ListBox1.Items.RemoveAt(i);
            }
            else
                i++;
         }
  ...

Thank u man i was looking for this solution for days

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.