问题描述:

For the past few months I have been developing an Exam Generator website through VB.Net. Now I am in the implementation of (Take Exam) page in which the user will take his own exam and then he will be redirected to another page to get the results.

Take exam page is created dynamically (radio buttons, labels and drop down menus for the choices selected by the user), and the values of the questions and answers are retrieved from an SQL Server Database.

Also, this page whenever the user try to access it, or refresh it, the questions and answers will be shuffled. I implemented the shuffle through an SQL command (ORDER BY NEWID()).

My problem is that I need to process the answers of the user once he clicks submit button, but it's really difficult because whenever the user clicks submit the page will postback with different values for each question and answer. I tried to use AJAX but it couldn't help because the sub has to be protected in order to access the elements of the page through Page.FindControl() method.

Update: I added partial of the code for more explanation.

If (Request.Cookies("user") Is Nothing) Then

Response.Redirect("~/index.aspx", False)

ElseIf Request.UrlReferrer = Nothing Then

Session.Abandon()

Response.Redirect("~/main.aspx", False)

Else

.

.

.

.

sql = "SELECT * FROM examTime.dbo.questionexam WHERE examID = @eID ORDER BY type, NEWID()"

cmd.CommandText = sql

da.SelectCommand = cmd

Try

da.Fill(dtall)

Variables.Variables.dtAll = dtall.Clone()

For i As Integer = 0 To dtall.Rows.Count - 1 Step 1

Variables.Variables.dtAll.ImportRow(dtall.Rows(i))

Next

Catch ex As Exception

MsgBox(ex.Message)

End Try

If mcqCount > 0 Then

Dim dtmcq As New DataTable

dtmcq.Columns.AddRange(New DataColumn(3) {New DataColumn("id", GetType(Integer)), New DataColumn("examID", GetType(Integer)), New DataColumn("questionID", GetType(Integer)), New DataColumn("type", GetType(Integer))})

Dim mcqID As Integer

Dim dtmcqQC As New DataTable

Dim dtmcqAns As New DataTable

Try

panelQContainer.Controls.Add(New LiteralControl("<div class='text blue uppercase'>Multiple Choice Questions</div><div class='text grey uppercase small'>"" Choose the correct answer. ""</div>"))

panelQContainer.Controls.Add(New LiteralControl("<div class='review-container-content'>"))

For n As Integer = 1 To mcqCount Step 1

dtmcq.ImportRow(dtall.Rows(0))

dtall.Rows.RemoveAt(0)

dtall.AcceptChanges()

mcqID = Integer.Parse(dtmcq.Rows(n - 1).Item(2))

sql = "SELECT question, correct FROM examTime.dbo.question WHERE id = @qid"

qidP.Value = Integer.Parse(mcqID)

cmd.CommandText = sql

da.SelectCommand = cmd

da.Fill(dtmcqQC)

panelQContainer.Controls.Add(New LiteralControl("<div class='text red uppercase underline'>Question #" & n & ":</div>"))

panelQContainer.Controls.Add(New LiteralControl("<div class='text black'>" & dtmcqQC.Rows(0).Item(0) & "</div>"))

panelQContainer.Controls.Add(New LiteralControl("<table class='t-max'>"))

panelQContainer.Controls.Add(New LiteralControl("<tr><td class='text grey uppercase'><input type='radio' name='mcq" & n & "' value='A' />&nbsp;Answer A.</td>"))

panelQContainer.Controls.Add(New LiteralControl("<td class='text grey uppercase'><input type='radio' name='mcq" & n & "' value='B' />&nbsp;Answer B.</td>"))

panelQContainer.Controls.Add(New LiteralControl("<td class='text grey uppercase'><input type='radio' name='mcq" & n & "' value='C' />&nbsp;Answer C.</td>"))

panelQContainer.Controls.Add(New LiteralControl("<td class='text grey uppercase'><input type='radio' name='mcq" & n & "' value='D' />&nbsp;Answer D.</td></tr>"))

sql = "SELECT qAnswer FROM examTime.dbo.answer WHERE questionID = @qid ORDER BY NEWID()"

cmd.CommandText = sql

da.SelectCommand = cmd

da.Fill(dtmcqAns)

Dim arrAns As String()

arrAns = {dtmcqQC.Rows(0).Item(1), dtmcqAns.Rows(0).Item(0), dtmcqAns.Rows(1).Item(0), dtmcqAns.Rows(2).Item(0)}

arrAns = Functions.Functions.Shuffle(arrAns)

panelQContainer.Controls.Add(New LiteralControl("<tr><td class=""text blue"">" & arrAns(0) & "</td>"))

panelQContainer.Controls.Add(New LiteralControl("<td class=""text blue"">" & arrAns(1) & "</td>"))

panelQContainer.Controls.Add(New LiteralControl("<td class=""text blue"">" & arrAns(2) & "</td>"))

panelQContainer.Controls.Add(New LiteralControl("<td class=""text blue"">" & arrAns(3) & "</td></table>"))

If Not n = mcqCount Then

panelQContainer.Controls.Add(New LiteralControl("<div class='separator dark'></div>"))

End If

dtmcqQC.Clear()

dtmcqAns.Clear()

Next

panelQContainer.Controls.Add(New LiteralControl("</div>"))

Catch ex As Exception

MsgBox(ex.Message)

End Try

End If

End If

Your help is highly appreciated.

Thanks.

相关阅读:
Top