Solved

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

Posted on 2006-07-04
9
424 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now