...

View Full Version : QueryString Help



mcw92
03-10-2012, 08:53 PM
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




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 :




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.

SouthwaterDave
03-24-2012, 06:53 PM
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:


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum