我试着用ASP.Net和C#做一份调查问卷,这是一个问题列表,基于每一个答案,会出现另一个问题(例如,这是X还是Y?如果你从单选按钮列表中选择X,你会得到另一个问题“x的颜色是什么”,如果你选择Y "hos fast“)
到目前为止,我制作了大约7个带有无线电按钮列表(问题数量)的标签,将它们设置为不可见,根据第一个问题的答案,我使第二个标签和单选按钮列表可见,并将文本更改为我想要的问题。
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
}到目前为止,这个逻辑运行得很好,但是还有比这更简洁的逻辑吗?例如,在回答了第7号问题之后,我对第4个问题作了修改,如何才能使回答的问题(5,6,7)再次被清除和隐藏?
发布于 2016-01-13 20:34:07
我不能提供有关这方面的代码,但这是一个可能的想法,您可能会发现有用。
从你的描述中,我想到了一个数据结构。Tree。看起来,您的问题可以通过将问题实现到Tree数据结构中,使用面向对象的编程来实现。
树中的每个节点都可以有一个唯一的标识符(即ID),这将使您的单选按钮与树链接成为可能。
如果我们有问题A和问题B,而B取决于问题A,那么B问题将是A问题的子问题。
因此,您可以通过将每个已访问节点标记为Visited来根据用户的选择遍历树,这将帮助您跟踪用户当前所遵循的“路径”。
下面是树节点的一个可能的(草案)结构:
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(...)
{
....
}
.
.
.
}https://stackoverflow.com/questions/34776320
复制相似问题