【问题标题】:Dynamic questionnaire动态问卷
【发布时间】:2016-01-13 20:22:10
【问题描述】:

我正在尝试使用 ASP.Net 和 C# 制作问卷 它是一个问题列表,根据每个答案会出现另一个问题(例如,这是 X 还是 Y?如果您从单选按钮列表中选择 X,您将收到另一个问题“x 的颜色是什么”,如果您选择 Y“hos快是Y")

到目前为止,我制作了大约 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)再次清除和隐藏?!!!

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    我无法提供有关此的代码,但这是一个可能对您有用的想法。

    根据您的描述,我想到了一种数据结构。 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(...)
       {
         ....
       }
       .
       .
       .
    }
    

    【讨论】:

    • 你是对的!,这是一个绝妙的主意,我现在正在努力
    • 我很高兴能帮上忙
    猜你喜欢
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多