Reputation: 25
I'm a noob progammer that is working on something for our company. I'm working on a Quiz engine for training. I have some sample code that is about 90% built. I just need to tweak a few things. Here's what I'm working on.
THis is built in ASP.net with VB. I have a set of questions that I"m pulling from a database (using the built-in SQLDataSource binding). Currently what it does is pull the question, you select the answer, and click Next. It then pulls the next question in the list and so forth....till the end. The Database contains a column that indicates what the correct answer is. When you click next, it compairs your answer to the correct answer, stores it, then continues to the next question. At the end, it spits out your correct answers and incorrect answers.
However, this is what I want to do. When the user selects an answer and clicks next, it immediately opens up a small new window (not a pop-up, but a window on the same page) that immediately "grades" that question and in that window, displays whether it's correct..something like this:
If selected answer = correctAnswer then
"That is correct"
Else
"THat is not correct. The correct answer is B"
End if
The new window will only contain an "OK" button in the bottom corner. When the OK is pressed, it closes that new window and processes the rest of what the "next" button is programmed to do. Here is the button:
<asp:Button ID="buttonNext" runat="server" Text="Next" /> </td>
Here is the Questions.aspx.VB code to go along with that:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonNext.Click
' Save off previous answers
Dim dr As System.Data.DataRowView
dr = CType(questionDetails.DataItem, System.Data.DataRowView)
' Create Answer object to save values
Dim a As Answer = New Answer()
a.QuestionID = dr("QuestionOrder").ToString()
a.CorrectAnswer = dr("CorrectAnswer").ToString()
a.UserAnswer = answerDropDownList.SelectedValue.ToString()
Dim al As ArrayList
al = CType(Session("AnswerList"), ArrayList)
al.Add(a)
Session.Add("AnswerList", al)
If questionDetails.PageIndex = questionDetails.PageCount - 1 Then
' Go to evaluate answers
Response.Redirect("results.aspx")
Else
questionDetails.PageIndex += 1
End If
If questionDetails.PageIndex = questionDetails.PageCount - 1 Then
buttonNext.Text = "Finished"
End If
End Sub
If you are able to provide the code I need, that will be helpful. Thanks in advance for the help.
Tim
Upvotes: 2
Views: 4342
Reputation: 226
This should be fairly straight forward. As you have already retrieved the correct answer there is no need to do another call.
On your page you need to create a where you want the grading and OK button to live. Something like this would suffice:
<div id="gradeWindow" runat="server" visible="false">
<asp:label id="gradeLabel" runat="server" text="" />
<asp:button id="gradeOK" runat="server" text="OK" onclick="gradeOK_Clicked" />
</div>
Then modify your function to look like this
Session.Add("AnswerList", al)
If String.Compare(a.UserAnswer, a.CorrectAnswer) = 0 then
gradeLabel.Text = "That is correct"
Else
gradeLabel.Text = "That is not correct. The correct answer is " + a.CorrectAnswer
EndIf
gradeWindow.Visible = true
End Sub
Protected Sub gradeOK_Clicked(ByVal sender As Object, ByVal e As System.EventArgs)
If questionDetails.PageIndex = questionDetails.PageCount - 1
Then
Response.Redirect("results.aspx")
Else
questionDetails.PageIndex += 1
End If
If questionDetails.PageIndex = questionDetails.PageCount - 1
Then
buttonNext.Text = "Finished"
End If
End Sub
Upvotes: 2