Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    QueryString Help

    Hey,
    im building a simple quiz using asp.net.

    Im trying to pass on an int value from the Q1 page to the Q2 page,
    This is the code i have:

    Q1 Page

    Code:
    string sScore;
            int score = 0;
    
    
    protected void Page_Load(object sender, EventArgs e)
            {
    
                //Start Creating Question//
                lblQuestion.Text = "What is the capital city of Ireland?";
    
                if (!IsPostBack)
                {
                    ArrayList list1 = new ArrayList();
                    list1.Add(new ListItem("Dublin", "1"));
                    list1.Add(new ListItem("Cork", "2"));
                    list1.Add(new ListItem("Sligo", "3"));
                    rbl.DataSource = list1;
                    rbl.DataBind();
                }
                //End Creating Question//
    
     protected void btnNext_Click(object sender, EventArgs e)
            {
                Response.Redirect("Q2.aspx?score=" + score);
            }
    
           
    
            protected void rbl_SelectedIndexChanged(object sender, EventArgs e)
            {
               
                   
                   
    
                    if (rbl.SelectedIndex == 0)
                    {
                        score++;
                        //score=1//
                    }
                    
                    lblScore.Text = Convert.ToString(score);
                    int.TryParse(Request.QueryString["score"], out score);
    
    
                   // sScore = (string)Request.QueryString["score"];
                           
                    //stringscore = Convert.ToString(intscore);
                    //lblScore.Text = stringscore;
                   
                    //stringscore = (string)Request.QueryString["score"];
    Q2 Page :

    Code:
     
    
    string sScore;
            int selection1; int selection2; int selection3; int selection4; int selection5;
            int score;
    protected void Page_Load(object sender, EventArgs e)
            {
    
    
    
                //Start Creating Question//
                lblQuestion.Text = "Who is Barack Obama?";
    
                if (!IsPostBack)
                {
                    ArrayList list2 = new ArrayList();
                    list2.Add(new ListItem("Irish President", "1"));
                    list2.Add(new ListItem("American President", "2"));
                    list2.Add(new ListItem("Batman", "3"));
                    rbl.DataSource = list2;
                    rbl.DataBind();
                }
                //End Creating Question//
    
    
               
                
                //newscore = (string)Request.QueryString["score"];
                //score = (string)Request.QueryString["score"];
    
                score = int.TryParse(Request.QueryString["score"], out score);
                lblScore.Text = Convert.ToString(score);
    
    ^^Should show the score from previous page, where if index 0(dublin) was picked, it would be 1.
    
            }

    So in basics, on Q1 page, int score is created, given a value of 1 when dublin is clicked(a label on Q1 page proves this), then i want to pass on score to Q2(keeping the value of 1), and show the score on a label on Q2 also(still 1)

    It can be as simple as you like as this is for personal use.
    Also if anyone has another way of storing the selected value from the radiobuttonlist, please add it
    I have 5 q's and the score needs to be totaled at the final page.

  • #2
    Regular Coder
    Join Date
    May 2007
    Location
    UK
    Posts
    180
    Thanks
    0
    Thanked 18 Times in 18 Posts
    I wouldn’t use the query string in this manner. Someone taking the test could easily change the address in their browser to Q2.aspx?score=5000000, for instance.

    A better way to put together a 5 question quiz would probably be to use a MultiView or a Wizard control on a single page.

    However, if you wish to continue with your current design then you will need to resolve the problem in Q1.aspx. You have a rbl_SelectedIndexChanged event handler and a btnNext_Click event handler, but which fires first? Try this instead:
    Code:
    protected void btnNext_Click(object sender, EventArgs e)
    {
     if (rbl.SelectedIndex == 0)
     {
        score++;
     }
     Response.Redirect("Q2.aspx?score=" + score.ToString());
    }
    and delete the rbl_SelectedIndexChanged event handler.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •