?
Solved

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

Posted on 2006-07-04
9
Medium Priority
?
432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

752 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