Solved

Dynamically changing checkboxlist into a radiobuttonlist - a better approach??

Posted on 2006-07-04
9
429 Views
Last Modified: 2011-09-20
I have a scenario whereby the user needs to choose values from a list.  On occasions, only 1 value is needed.  On other occasions, multiple values are required.

Currently I am using both a radioButtonList and a checkBoxList, and display the correct list each time depending on the requirements.  To save code however, is it possible to use only 1 list, and change it's type each time?  Or is there a better way to accomplish this??
0
Comment
Question by:Rouchie
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 9

Expert Comment

by:zaghaghi
ID: 17037189
hi,

You  can use ListBox, and simply change the SelectionMode property to Single or Multiple
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 17037217
Or you can use CheckBoxList, and insert a javascript code, to prevent single selection
0
 
LVL 25

Author Comment

by:Rouchie
ID: 17037351
zaghaghi,
For aesthetics and simplicity it needs to be either a checkBoxList or RadioButtonList.  Is there an example of the javascript approach that I can view?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:lojk
ID: 17037414
yuk!

The best single option is to create a usercontrol (with a radiolist and checkboxlist together on the control and a public property called mode to flick between the two) to do the job for you and add that usercontrol instead o the checkboxlist and radiolist and then do a MyMixedControl.Mode=enRadioList as required

This will require a little bit of 'skill up' if youve never done a usercontrol before but is a far superior way to do it.. could probably knock up some code if you are interested inthe idea however.

The other option is to use a regular listbox and set SelectionMode as required, but this may require showing your users how to multiple select (i usually put this as a tooltip on the listbox) by using control key but then most people actually expect this behaviour in a listbox anyway

One Last Thought, you could present with a combobox instead of a radiolistbox to single select (again may work better within the usercontrol idea)
0
 
LVL 9

Accepted Solution

by:
Joeisanerd earned 500 total points
ID: 17037588
RadioButtonList controls and CheckBoxList controls both come from the ListControl class create a member variable of type ListControl  like protected ListControl myLC = null;  

Then in your initializing code that determines which type of control it is going to be set it to the right type like

       if ( some condition indicating Radio buttonlist)
        {
            myLC = new RadioButtonList();
           
        }
        else
        {
            myLC = new CheckBoxList();
        }
        // then just databind it like normal
        myLC.DataSource  = myDS;
        myLC.DataBind();

        // Then add it to the form via a place holder control,
        this.PlaceHolder1.Controls.Add(lc);

The benefits of this way is that you can access the ListControls Items collection and check the Selected property to see which ones where selected because it maintains the ViewState

You can always wire up the events for the listcontrol for selected index changed and things like that if you still need to.
0
 
LVL 9

Expert Comment

by:Joeisanerd
ID: 17037606
The above solution does allow for you to only have one list to maintain and one set of events.

to wire the event just for the listcontrol just do something like this

        myLC.SelectedIndexChanged += new EventHandler(myLC_SelectedIndexChanged);
        myLC.AutoPostBack = true;

and implement the SelectedIndexChange method
    void myLC_SelectedIndexChanged(object sender, EventArgs e)
    {
        // do something
    }
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 17037759
I work on your question but, i can't find a good javascript that can help you

i also suggest the Joeisanerd Solution, it's better than my solution, because if the user disable javascript my solution doesn't  work
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 17041089
0
 
LVL 25

Author Comment

by:Rouchie
ID: 17049102
Okay so I managed to get the listControl to display as the correct type.  Now how do I read the posted values back in when the user submits the form each time...??
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21909655.html
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question