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;
        string correctanswer;
        public Random rnd = new Random();

       

        protected void Page_Load(object sender, EventArgs e)
        {

            questions = (string[])Session["Questions"];
            int randNum = rnd.Next(questions.Length);
            question = questions[randNum];

           

            lblQ1.Text = question;


            answers = (string[])Session["answers1"];
            correctanswer = answers[1];
            for (int i = 0; i < answers.Length; i++)
            {

                rbAns1.Items[i].Text = answers[i];

           
            }
        }
      protected void Page_PreRender(object sender, EventArgs e)
       {


           if (Session["selectedValue1"] != null)
           {

               rbAns1.SelectedIndex = (int)Session["selectedValue1"];

           }
           if (Session["questionAsked"] != null)
           {

               question = Session["questionAsked"].ToString();
               lblQ1.Text = question;

           }
            
      
       }

        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");
          
        }

Good Job, you've done wonderfully well. But what you just need to add is an 'IF..ELSE..' Statement. Btw line 15 and 16 add the following codes

IEnumerator ie = Session.GetEnumerator();
        ArrayList SessionArray = new ArrayList();
        while (ie.MoveNext())
        {
            SessionArray.Add(ie.Current);
        }
        if (SessionArray.Contains("questionAsked") And SessionArray.Contains("Asked"))
        {
            if (Session("Asked"))
            {
                lblQ1.text = (string) Session("Asked");
            }
            else
            {
                //your normall code to add questions
                Session.Add("Asked1", True);
            }
             //This will be repeated on all the page load function. While you change the session variable "Asked1" to suite page/question number. e.g Asked10 for page/question 10.
        }

Let me know if this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.