Reputation: 104
I'm trying to make a questionnaire using ASP.Net and C# it's a list of questions and based on each answer another question appears (e.g. Is this X or Y? if you choose X from the radio button list you'll get another question "what's the color of x" and if you choose Y "hos fast is Y")
what i made so far is that i made about 7 labels with radiobutton lists (number of questions) , set them to invisible, and based on the first question's answer i make the second label and radio button list visible, and change the text to the question i want.
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) //first question
{
//selected value =0 => No || selected value= 1 => Yes
if (RadioButtonList1.SelectedValue == "1")
{
Label1.Visible = true;
Label1.Text = "Question number 2 based on answer a"; //show the second question
RadioButtonList2.Visible = true; //show the second question options
}
else {
Label1.Text = "Question number 2 based on answer b";
Label1.Visible = true; //show the second question
RadioButtonList2.Visible = true; //show the second question options
}
}
protected void RadioButtonList2_SelectedIndexChanged(object sender, EventArgs e) //2nd question
{
if (RadioButtonList1.SelectedValue == "1" && RadioButtonList2.SelectedValue == "1") //1st question answer was yes &2nd is yes
{
//some logic here
}
it's working fine till now with this logic, but is there's a more neat logic than this ? and after answering question number 7 for example and then i made a modification in question number 4, how can i make the answered questiones (5,6,7) cleared and hidden again?!!!
Upvotes: 3
Views: 1428
Reputation: 7242
I can't provide code about this, but is a possible idea that you may find useful.
From your description one data structure came to my mind. Tree
. It looks like your problem can be implemented using Object Oriented programming by implementing your questions into a Tree
data structure.
Each node in the Tree can have a unique identifier (i.e ID) this will make possible to link your radio-buttons with the Tree.
If we have question A
and question B
, and B
is depending on the question A
, then B
question will be child of A
question.
So you could then depending on the user's option to traverse the Tree by marking each visited node as Visited
this will help you to keep track of the "path" that the user currently is following.
Here is a possible (draft) structure of your Tree node:
internal class Node
{
private int ID { get; private set; }
private string NodeQuestion { public get; public set; }
private int State { public get; public set; } // 0 for Non-Visited, 1 for Visited
private Node Left { public get; public set; }
private Node Right { public get; public set; }
public Node(...)
{
....
}
.
.
.
}
Upvotes: 3