Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

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

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
Rouchie
Asked:
Rouchie
  • 4
  • 2
  • 2
  • +1
1 Solution
 
Hamed ZaghaghiProgrammerCommented:
hi,

You  can use ListBox, and simply change the SelectionMode property to Single or Multiple
0
 
Hamed ZaghaghiProgrammerCommented:
Or you can use CheckBoxList, and insert a javascript code, to prevent single selection
0
 
RouchieAuthor Commented:
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!

 
lojk.Net and Infrastructure ConsultantCommented:
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
 
JoeisanerdCommented:
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
 
JoeisanerdCommented:
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
 
Hamed ZaghaghiProgrammerCommented:
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
 
RouchieAuthor Commented:
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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now