Adding dynamic controls
I have a page, I need to build, where depending on the user's choice made in the form on the page, before it displays another set of questions that they can answer.
So tell the user to select reason A from the form to edit the page, then she needs to display questions 1 and 2 on the edit_confirmation page. But if they choose Reason B then it needs to display questions 3 and 4.
I grab the reason code from the query string and set a switch statement, but I can't find anywhere how to output the various controls. If questions 1 and 2 are supposed to be asked, there might be a text box and the other a checkbox, but if questions 3 and 4 are to be displayed, a dropdown and checkbox might be required.
EDIT: I'm going to try some of the suggestions below and return to bookmark the answer and refund accordingly. Thanks everyone for the quick response.
EDIT EDIT: The answers from rlb.usa and AndrewVos work equally well. I went with Andrew because it seemed like a more "correct" way to do it.
a source to share
Display various controls? Oh, that sounds very painful. I think the term you are looking for is dynamic controls (controls created in code). I've always had more problems with it than I should.
The most common way to do this is to customize all of your controls on the form. Let each "question" or related question asks everything contained in ASP:Panel
. Next, inside your code, all you have to do is apply your logic and hide / show ASP:Panel
(by setting a property Visible
) according to your needs. You can either do this logic on Page_Load
, if applicable, or when changing a specific answer.
a source to share
1 way would be to have the controls added statically. Hide or hide controls based on user selection. This approach is the easiest to implement, but rather ugly.
Another approach is to dynamically create controls and add them. You will have to think about managing the viewstate as these things can be quite frustrating.
a source to share