views:

154

answers:

6

I have a page I need to build out where depending on the selection the user made on a form on the page prior it displays a different set of questions for them to answer.

So say the user selects Reason A from the form on page edit, then it needs to display Questions 1 and 2 on page edit_confirmation. But if they select Reason B then it needs to display Questions 3 and 4.

I'm grabbing the reason code from the query string and have a switch statement set up, but I can't find anywhere how to output different controls. If Questions 1 and 2 are supposed to show up, one could be a text box and the other a checkbox, but if questions 3 and 4 are supposed to show up one may need to be a dropdown list and a checkbox.

EDIT: I'm going to try some of the below suggestions and will be back to mark the answer and upvote accordingly. Thank you all for the quick response.

EDIT EDIT: Both rlb.usa and AndrewVos's answers worked equally well. I went with Andrew's since it seemed like the more "proper" way of doing it.

+4  A: 

Output different controls? Ouch, that sounds very painful. I think the term you are looking for is dynamic controls (controls created within the code). I've always had more trouble with this than it's worth.

The most common practice way to do it is to set up all of your controls on the form. Have each "question" or relevant question sets all contained within an ASP:Panel. Next, inside your code, all you need to do is apply your logic and hide/show the ASP:Panels (by setting the Visible property) according to your needs. You can either do this logic on Page_Load, if applicable, or when a particular answer was changed.

rlb.usa
+4  A: 

Have a look at the MultiView control. It allows you to add Views and specify which one is visible according to your requirements.

AndrewVos
I think MultiView should be a good fit.
Yogendra
Agreed, multiview is the way to go. Best to set up an Enum with descriptive values that map to the indexes of each "view". Then you can write an easy method to switch the active view based on the enum.
Josh
What is the benefit of using MultiView over following what rlb.usa suggested below as far as setting panels or even individual controls to visible or not visible?
Mike Keller
MultiView is just easier to use, and you're sure that you're not going to forget to hide or show a Panel at some point. Also, AFAIK it doesn't generate any extra HTML (the Panel does). If you wanted to use rlb.usa's approach, then at least use PlaceHolders because they too don't generate any excess HTML.
AndrewVos
A: 

Why not just send them to a different page based on the reason they select on page edit?

David
This can get old quick if he has a lot of these branching question choices.
rlb.usa
@rlb.usa true, but is it any older than adding more and more controls to one page?
David
+1  A: 

You could use a PlaceHolder control and add the relevant controls to it on the server side, or you could render all of the controls with thier style.display set to none and set the relevant ones to "block" useing client side javascript

Daniel Dyson
A: 

1 way would be to have the controls added statically. Based on the user selection hide or unhide the controls. This approach is the easiest to implement but is kind of ugly.

Other approach is to dynamically create controls and add them. You will have to think of managing viewstate as these things can be pretty nasty.

Yogendra
A: 

You could simply set the visible property on the control in your page_load event. So in the switch statement, you would only make visible the controls that you want the user to see.

Sijin