在asp.net中一一显示问题以进行测验时出现问题 [英] Problem in displaying Question one by one for playing quiz in asp.net

查看:104
本文介绍了在asp.net中一一显示问题以进行测验时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net上比较新鲜.
在以下情况下,单击按钮时我想显示问题.我用于循环.但是问题是我只能显示最后一个问题和选项.但是,如果我使用response.write,则会显示所有3个问题和选项.我已将我的代码和输出放在下面.
有人可以帮我吗.

在program.cs

Hi I am a fresher in asp.net.
In the below situation i want to display question when the button is clicked. I used for loop. But the problem is i can able to display only the last question and option. But if i use response.write all the 3 question and option are displayed. I have placed my code and output below.
Can any one please help me on this.

In program.cs

protected void Button1_Click1(object sender, EventArgs e)
   {
       DatabaseDLL.Quiz ObjQuiz = DatabaseDLL.Manager.DisplayQuestions("General Knowledge");
       if (IsPostBack)
       {
           foreach (DatabaseDLL.Question Q in ObjQuiz.GetQuestions())
           {
               Response.Write(Q.Question1);
               Response.Write(Q.Option1);
               Response.Write(Q.Option2);

               Label1.Text = Q.Question1;
               Label2.Text = Q.Option1;
               Label3.Text = Q.Option2;

           }
       }

   }


在dll管理器类中:


In the dll manager class:

public static Quiz DisplayQuestions(string category)
      {
          Quiz ObjQuiz = new Quiz();
          try
          {
              Conn = new SqlConnection("Data Source=HDCHARTOMR1213\\SQLEXPRESS;Initial Catalog=UserDetails;Integrated Security=SSPI");
              Conn.Open();
              SqlCommand Cmd = new SqlCommand("QuizQuestions", Conn);
              Cmd.CommandType = CommandType.StoredProcedure;
              Cmd.Parameters.AddWithValue("@Category", category);
              SqlDataReader reader = Cmd.ExecuteReader();
              while (reader.Read())
              {

                  Question ObjQuestion = new Question(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString());
                  ObjQuiz.AddQuestion(ObjQuestion);
              }

              reader.Close();

          }
          catch (Exception e)
          {
              Console.WriteLine(e);
          }
          finally
          {
              if (Conn != null)
              {
                  Conn.Close();
              }
          }
          return ObjQuiz;

      }


问题类别:


Question Class:

public class Question
   {
        public string Option1 { get; private set; }
        public string Option2 { get; private set; }
        public string Question1 { get; private set; }
        public string Category { get; private set; }
        public Question(string Question1, string Option1, string Option2, string Category)
        {
            this.Question1 = Question1;
            this.Option1 = Option1;
            this.Option2 = Option2;
            this.Category = Category;
        }

   }


测验类别:


Quiz Class:

public class Quiz
    {
       public List<question> ObjQuestion = new List<question>();
       public void AddQuestion(Question NewQuestion)
       {
           ObjQuestion.Add(NewQuestion);
       }
       public List<question> GetQuestions()
       {
           return ObjQuestion;
       } 
        public Quiz()
        {
           
        }
    }


输出:

在Button单击之后,我得到以下输出:

//for response.write
谁是我们国家的父亲
圣雄甘地
贾瓦哈拉尔·尼赫鲁
什么是我们的国家动物
狮子
老虎
什么是国花
莲花
玫瑰



//对于标签,仅显示最后一个问题和选项
什么是我们的国花//label1

莲花//label2

玫瑰//label3

谢谢!!


Output:

After Button click i get the following output below:

//for response.write
Who is the Father of our Nation
Mahathma Gandhi
Jawaharlal Nehru
What is our National Animal
Lion
Tiger
What is our National Flower
Lotus
Rose



//for the label only last Question and options are displayed
What is our National Flower //label1

Lotus //label2

Rose //label3

Thank You!!

推荐答案

它不能那样工作.

当您使用forforeach循环时,它会完全执行-因此,您将在短时间内显示每个问题,然后仅显示最后一个问题-它覆盖了所有其他问题.

您需要做的是拥有一个Session变量或cookie(取决于应用程序的工作方式),该变量或cookie存储您所处的问题(可能还有一个单独的变量来记录到目前为止的问题).然后,您阅读并仅显示该问题.

由于这是您的家庭作业,因此我不会给您任何代码:但是它并不复杂,无论如何您的讲义都应该涵盖.
It doesn''t work like that.

When you use a for or foreach loop it executes completely - so you will display each of the questions for a minute period of time before showing only the last one - it having overwritten all the others.

What you need to do is have a Session variable or cookie (depending on how your application is meant to work) which stores the question you are on (and probably a separate one which records the answers so far). You then read that and display only that question.

Since this is your homework, I won''t give you any code: but it''s not complex, and should be covered by your lecture notes anyway.


我尝试过.但是我不明白.
如果有人可以找到解决方案,请帮帮我..
谢谢!
I tried. But i am not getting it..
If any one can find solution help me..
Thanks!


使用executereader()从数据库中获取值并将其存储在DataTable中.
现在,从DataTable中,我们可以获取所需的列并显示在文本框和单选按钮中.
解决方案变得很简单.
Get the values from database using executereader() and store it in DataTable.
Now from the DataTable we can get the columns we want and display in textbox and radiobutton.
The solution has become simple..


这篇关于在asp.net中一一显示问题以进行测验时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆