Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

asp.net quiz

Options
  • 10-03-2012 2:41pm
    #1
    Registered Users Posts: 46


    Hi, I'm new to asp.net, I'm creating a quiz, I have a set of questions that are stored in an array and they are stored in session state, when I load the first page of the quiz a question is randomly chosen with a set of answers. when I move forward a page and then back
    I want the same question to be displayed but it keeps displaying a new question, I tried storing the outputted question in session and then loading it before, but it doesn't work any suggestions???

    Thanks in advance
    namespace Quiz
    {
        public partial class page2 : System.Web.UI.Page
        {
            string question;
            string[] questions;
            string[] answers;
           
            public Random rnd = new Random();
    
            protected void Page_Init(object sender, EventArgs e)
            {
                
    
                    questions = (string[])Session["Questions"];
                    int randNum = rnd.Next(questions.Length);
                    question = questions[randNum];
                   
              
            }//End init
           
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
    
                if (Session["selectedValue1"] != null)
                {
    
                    question = Session["questionAsked"].ToString();
                    lblQ1.Text = question;
                    rbAns1.SelectedIndex = (int)Session["selectedValue1"];
                }
    
    
                lblQ1.Text = question;
              
                
    
                answers = (string[])Session["Answers1"];
                for (int i = 0; i < answers.Length; i++)
                {
    
                    rbAns1.Items[i].Text = answers[i];
    
                }
                   
    
    
                
              
                
            }
         
            protected void rbAns1_SelectedIndexChanged(object sender, EventArgs e)
            {
                Session["selectedValue1"] = rbAns1.SelectedIndex;
               
               
            }
    
            protected void btnPrevious_Click(object sender, EventArgs e)
            {
    
                Session["questionAsked"] = question;
                Response.Redirect("StartPage.aspx");
            }
    
            protected void btnNext_Click(object sender, EventArgs e)
            {
    
                Session["questionAsked"] = question;
                Response.Redirect("page3.aspx");
              
            }
    
    
        }
    }
    


Comments

  • Registered Users Posts: 851 ✭✭✭TonyStark


    rambo85 wrote: »
    Hi, I'm new to asp.net, I'm creating a quiz, I have a set of questions that are stored in an array and they are stored in session state, when I load the first page of the quiz a question is randomly chosen with a set of answers. when I move forward a page and then back
    I want the same question to be displayed but it keeps displaying a new question, I tried storing the outputted question in session and then loading it before, but it doesn't work any suggestions???

    Thanks in advance
    namespace Quiz
    {
        public partial class page2 : System.Web.UI.Page
        {
            string question;
            string[] questions;
            string[] answers;
           
            public Random rnd = new Random();
    
            protected void Page_Init(object sender, EventArgs e)
            {
                
    
                    questions = (string[])Session[&#34;Questions&#34;];
                    int randNum = rnd.Next(questions.Length);
                    question = questions[randNum];
                   
              
            }//End init
           
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
    
                if (Session[&#34;selectedValue1&#34;] != null)
                {
    
                    question = Session[&#34;questionAsked&#34;].ToString();
                    lblQ1.Text = question;
                    rbAns1.SelectedIndex = (int)Session[&#34;selectedValue1&#34;];
                }
    
    
                lblQ1.Text = question;
              
                
    
                answers = (string[])Session[&#34;Answers1&#34;];
                for (int i = 0; i &#60; answers.Length; i++)
                {
    
                    rbAns1.Items[i].Text = answers[i];
    
                }
                   
    
    
                
              
                
            }
         
            protected void rbAns1_SelectedIndexChanged(object sender, EventArgs e)
            {
                Session[&#34;selectedValue1&#34;] = rbAns1.SelectedIndex;
               
               
            }
    
            protected void btnPrevious_Click(object sender, EventArgs e)
            {
    
                Session[&#34;questionAsked&#34;] = question;
                Response.Redirect(&#34;StartPage.aspx&#34;);
            }
    
            protected void btnNext_Click(object sender, EventArgs e)
            {
    
                Session[&#34;questionAsked&#34;] = question;
                Response.Redirect(&#34;page3.aspx&#34;);
              
            }
    
    
        }
    }
    

    Why not have one page to handle the quiz? Wouldn't that make it a little easier?


  • Registered Users Posts: 46 rambo85


    only allowed one question per page im afraid!


  • Registered Users Posts: 46 rambo85


    This is the first page

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Quiz
    {
        public partial class StartPage : System.Web.UI.Page
        {
            //Array of Questions 
            string[] Questions = new string[] { "At Dartmouth College in 1964 John Kemeny and Thomas Kurtz invented .... ?",
                                                "Complete this quote &#8220;Computers are useless. They can only give you -------",                                           
                                                "Who invented C++ ?",
                                                "Which of these is not a functional programming language?",
                                                "A true or false statement put into code that the programmer expects to always be true is an ..." };
           
            
            //Array Of Answers
            string[] Answers1 = new string[] { "Algol","Basic","Fortran","C#" };
            string[] Answers2 = new string[] { "Numbers", "Headaches", "Noise", "Answers" };
            string[] Answers3 = new string[] { "Larry Wall", "Grace Hopper", "Bjarne Stroustrup", "Alan Cooper" };
            string[] Answers4 = new string[] { "Fortran", "F#", "Lisp", "Joy" };
            string[] Answers5 = new string[] { "Exception", "Harness", "Assertion", "Expression" };
    
            
           
            //Array Of Correct Answers
            string[] correctAnswers = new string[] { "Basic","Answers","Bjarne Stroustrup","Fortran","Assertion" };
            
            
            protected void Page_Load(object sender, EventArgs e)
            {
    
             
    
            }//End Page Load
    
            protected void btnStart_Click(object sender, EventArgs e)
            {
                string Fname = txtName.Text;
                string Lname = txtSurname.Text;
    
                Session["Fname"] = Fname;
                Session["Lname"] = Lname;
                Session["Questions"] = Questions;
                Session["answers1"] = Answers1;
                Session["answers2"] = Answers2;
                Session["answers3"] = Answers3;
                Session["answers4"] = Answers4;
                Session["answers5"] = Answers5;
                Session["correctAnswers"] = correctAnswers;
    
                Response.Redirect("page2.aspx");
    
            }//End Start Button
        }
    }
    
    


  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    Use query strings!
    private string [] questions = { "Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10" };
            private string [] answers = { "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10" };
            private int[] order = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                if (null == Session["Questions"])
                {
                    order = order.OrderBy(o => Guid.NewGuid()).ToArray();
                    //Stuff the order into the session.
                    Session.Add("Questions", order);
                    Response.Redirect("~/Default.aspx?page=1");
                }
                else 
                {
                    order = (int[])Session["Questions"];               
                }
                
    
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                int page = 0;           
                if (Request.QueryString["page"] != null)
                {
                    int.TryParse(Request.QueryString["page"], out page);
                }
                if (page != 0)
                {
                    Label1.Text = questions[order[page]];
                }
                
            }
    

    You just need to add paging which is trivial (next link = current page + 1). Also, error checking.

    It will load like a full page each time if you do it right.

    I see you use multiple answers per question, you can use the same method, as you don't have to shuffle the multiple choice (only the display of it, you can assign static values to the radio button or whatever you use it and compare against an array of answer keys). The order list always maintains to order the questions will be asked, how they relate to the answers and the answer key itself! In one simple variable! :)


Advertisement