CheckBoxList Items

Last post 03-24-2007 9:47 AM by kipo. 3 replies.

Sort Posts:

  • CheckBoxList Items

    03-16-2007, 11:25 AM
    • Loading...
    • david E
    • Joined on 01-28-2007, 5:23 PM
    • Posts 1

    hi

    i am sure this is a very easy one for you guys:

    in my checboxlist there are 10 items.

    i want to limit the user to choose (check) only 5 items out of these ten.

    when the user will check the sixth item, the fifth will become unchecked and the sixth will become checked.

    thanks

    david

     

  • Re: CheckBoxList Items

    03-23-2007, 6:26 AM
    • Loading...
    • JohanNL
    • Joined on 03-28-2003, 8:58 AM
    • Posts 1,053
    I think you have to use some client-side javascript for this....
    Johan Theunissen
    MCPD, MCSE, MCTS BizTalk 2006

    ==============================

    Please mark the most helpful reply/replies as "Answer".
  • Re: CheckBoxList Items

    03-23-2007, 11:49 AM
    • Loading...
    • kipo
    • Joined on 07-20-2006, 3:10 AM
    • Croatia
    • Posts 1,603

    You can try with this:

        protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int a = 0;
            foreach (ListItem b in CheckBoxList1.Items)
            {
                if (b.Selected == true)
                {
                    a++;
                }
            }
            if (a > 5)
                foreach (ListItem b in CheckBoxList1.Items)
                {
                    if (a > 5 && b.Selected == true)
                    {
                        b.Selected = false;
                        a--;
                    }
                }
        }

  • Re: CheckBoxList Items

    03-24-2007, 9:47 AM
    • Loading...
    • kipo
    • Joined on 07-20-2006, 3:10 AM
    • Croatia
    • Posts 1,603

    Or this:

        protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Random ran = new Random();
            int a = 0;
            foreach (ListItem b in CheckBoxList1.Items)
            {
                if (b.Selected == true)
                {
                    a++;
                }
            }
            if (a > 5)
            {
                while (a > 5)
                {
                    int broj = ran.Next(0, 9);
                    if (CheckBoxList1.Items[broj].Selected == true)
                    {
                        CheckBoxList1.Items[broj].Selected = false;
                        a--;
                    }
                }
            }
        }

Page 1 of 1 (4 items)
Microsoft Communities
Page view counter