Mike Keller
Mike Keller

Reputation: 615

Adding Dynamic Controls

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.

Upvotes: 2

Views: 593

Answers (6)

Sijin
Sijin

Reputation: 4550

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.

Upvotes: 0

AlwaysAProgrammer
AlwaysAProgrammer

Reputation: 2919

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.

Upvotes: 0

Daniel Dyson
Daniel Dyson

Reputation: 13230

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

Upvotes: 1

BlackICE
BlackICE

Reputation: 8926

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

Upvotes: 0

AndrewVos
AndrewVos

Reputation: 1295

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

Upvotes: 4

rlb.usa
rlb.usa

Reputation: 15041

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.

Upvotes: 4

Related Questions