Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-07-04
9
Medium Priority
?
435 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:Hamed Zaghaghi
ID: 17037189
hi,

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

Expert Comment

by:Hamed 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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:Hamed 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:Hamed 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

916 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