📄 exam.aspx.vb
字号:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents txtLastName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
Protected WithEvents lblHeading As System.Web.UI.WebControls.Label
Protected WithEvents lblLastName As System.Web.UI.WebControls.Label
Protected WithEvents lblPassword As System.Web.UI.WebControls.Label
Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
Protected mintComplete(9), mintActCount As Integer
Protected WithEvents plhPlaceHolder As System.Web.UI.WebControls.PlaceHolder
Protected mrdrReader As OleDb.OleDbDataReader
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal objSender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Page_Load is used in this lab to display the exam and
'to grade exam when exam is submitted
If Page.IsPostBack Then
'Hide Login Controls
Call ControlsStatus()
Dim strStatusResult As String = CheckStudent()
'If Exam not taken, generate/Grade Exam, else
'notify student and do nothing else
If strStatusResult = "Yet to Take Exam" Then
'If Exam submitted update session variable
If Request.Form("btnExamSubmit") <> Nothing Then
Session("ExamPosted") = "True"
End If
'Use session variable value to generate or grade exam
If Session("ExamPosted") <> "True" Then
Response.Write("<h3>Student Name: " + txtLastName.Text)
Response.Write(" Student ID: " + txtPassword.Text + "</h3>")
Response.Write("<hr>")
Response.Write("<u><b>Answer all of the following questions</u></b><br>")
Call MakeExam()
Else
Call GradeExam()
'Kill the session
Session.Abandon()
End If
Else
Response.Write(strStatusResult)
End If
End If
End Sub
Private Sub ControlsStatus()
'Make the Login controls invisible
lblHeading.Visible = False
lblLastName.Visible = False
lblPassword.Visible = False
txtPassword.Visible = False
txtLastName.Visible = False
btnSubmit.Visible = False
End Sub
Private Function CheckStudent() As String
'This routine checks whether student exists and if so whether exam
'had already been taken or not
'Dim strResult As String
Dim conConnection As New _
OleDb.OleDbConnection(ConfigurationSettings.AppSettings("DataConnection"))
Dim cmdCommand As New OleDb.OleDbCommand()
Dim strSQL As String = "Select * From StudentInformation" + _
" Where LastName = '" & txtLastName.Text & "' And StudentID = '" + _
txtPassword.Text + "'"
cmdCommand.Connection = conConnection
cmdCommand.CommandText = strSQL
conConnection.Open()
mrdrReader = cmdCommand.ExecuteReader()
'Check whether record exists and if so student exam status
If mrdrReader.Read() Then
If mrdrReader("AppearedForExam") = True Then
CheckStudent = mrdrReader("FirstMiddle") + _
" " + txtLastName.Text + "<br>" + _
"You already took the Exam!"
Else
CheckStudent = "Yet to Take Exam"
End If
Else
CheckStudent = txtLastName.Text + "/" + txtPassword.Text + _
" Combination Incorrect"
End If
mrdrReader.Close()
conConnection.Close()
conConnection.Dispose()
cmdCommand.Dispose()
End Function
Private Sub MakeExam()
'This Procedure Generates the Exam from Database
Dim intQueNumber, intCount As Integer
Dim bolFound As Boolean
'Seed for the random number
Randomize()
'Loop to generate 10 questions
Do While mintActCount <= 9
intQueNumber = CInt(Int((10 * Rnd()) + 1))
bolFound = False
bolFound = CheckNumber(intQueNumber)
'If the question is not already in the exam, add it
If Not bolFound Then
Call WriteQuestion(intQueNumber)
End If
Loop
Dim praPara As LiteralControl = New LiteralControl("<p>")
plhPlaceHolder.Controls.Add(praPara)
'Generate and add a button to submit exam
Dim btnExamSubmit As New Button()
btnExamSubmit.Text = "Grade Exam"
btnExamSubmit.ToolTip = "Submit Exam"
btnExamSubmit.ID = "btnExamSubmit"
plhPlaceHolder.Controls.Add(btnExamSubmit)
End Sub
Private Function CheckNumber(ByVal intQNbr As Integer) As Boolean
'Check Whether question Already included
Dim intCount, intCheck As Integer
For intCount = 0 To mintActCount - 1
If mintComplete(intCount) = intQNbr Then
'If question found, function returns true
Return True
Exit For
End If
Next
End Function
Private Sub WriteQuestion(ByVal intQNbr As Integer)
Dim intCount, intGrpQues As Integer
Dim strGroupID As String
Dim strSQL As String = "Select * From VisualBasicQuizBank " + _
"Where ID = " + intQNbr.ToString
Dim conConnection As New _
OleDb.OleDbConnection(ConfigurationSettings.AppSettings("DataConnection"))
Dim cmdCommand As New OleDb.OleDbCommand(strSQL, conConnection)
conConnection.Open()
mrdrReader = cmdCommand.ExecuteReader
mrdrReader.Read()
'Group value 1 indicates that it is not the first question
'in a group of questions. Hence, do nothing
If mrdrReader("Group") = 1 Then
mrdrReader.Close()
conConnection.Close()
conConnection.Dispose()
cmdCommand.Dispose()
Exit Sub
Else
'If Group value field is > 1, then need to generate
'more than one question. Else generate one question
If mrdrReader("Group") > 1 Then
intGrpQues = mrdrReader("Group") - mrdrReader("ID") + 1
Else
intGrpQues = 1
End If
End If
'Generate 1 or more questions
For intCount = 1 To intGrpQues
strGroupID = "A" + CStr(mrdrReader("ID"))
mintComplete(mintActCount) = mrdrReader("ID")
'Create and Add label. Label represent the question
Dim lblQuestion As New Label()
'Literal controls required for positioning purposes
Dim praPara As LiteralControl = New LiteralControl("<p>")
lblQuestion.Text = "<b>" + CStr(mintActCount + 1) + _
". " + mrdrReader("Question") + "</b>"
plhPlaceHolder.Controls.Add(praPara)
plhPlaceHolder.Controls.Add(lblQuestion)
Dim lbrSpacer As LiteralControl = New LiteralControl("<br>")
plhPlaceHolder.Controls.Add(lbrSpacer)
'Check to see whether type is Multiple choice or True/False
If mrdrReader("Type") <> 1 Then
'Declare 5 Radiobuttons for 5 choices
Dim optAnswerA As New RadioButton()
Dim optAnswerB As New RadioButton()
Dim optAnswerC As New RadioButton()
Dim optAnswerD As New RadioButton()
Dim optAnswerE As New RadioButton()
'Set properties of Radiobutton controls and
'add to plhPlaceHolder
optAnswerA.Text = "<b>A. </b>" + mrdrReader("A") + "<br>"
optAnswerA.GroupName = strGroupID
optAnswerA.ID = strGroupID + "A"
plhPlaceHolder.Controls.Add(optAnswerA)
optAnswerB.Text = "<b>B. </b>" + mrdrReader("B") + "<br>"
optAnswerB.GroupName = strGroupID
optAnswerB.ID = strGroupID + "B"
plhPlaceHolder.Controls.Add(optAnswerB)
optAnswerC.Text = "<b>C. </b>" + mrdrReader("C") + "<br>"
optAnswerC.GroupName = strGroupID
optAnswerC.ID = strGroupID + "C"
plhPlaceHolder.Controls.Add(optAnswerC)
optAnswerD.Text = "<b>D. </b>" + mrdrReader("D") + "<br>"
optAnswerD.GroupName = strGroupID
optAnswerD.ID = strGroupID + "D"
plhPlaceHolder.Controls.Add(optAnswerD)
optAnswerE.Text = "<b>E. </b>" + mrdrReader("E")
optAnswerE.GroupName = strGroupID
optAnswerE.ID = strGroupID + "E"
plhPlaceHolder.Controls.Add(optAnswerE)
Else
'Declare 2 Radiobuttons for True/False choices
Dim optAnswerA As New RadioButton()
Dim optAnswerB As New RadioButton()
optAnswerA.Text = "<b>A. </b>" + "True" + "<br>"
optAnswerA.GroupName = strGroupID
optAnswerA.ID = strGroupID + "A"
plhPlaceHolder.Controls.Add(optAnswerA)
optAnswerB.Text = "<b>B. </b>" + "False"
optAnswerB.GroupName = strGroupID
optAnswerB.ID = strGroupID + "B"
plhPlaceHolder.Controls.Add(optAnswerB)
End If
mrdrReader.Close()
If intGrpQues > 1 And intCount < intGrpQues Then
'Select the next question in group
strSQL = "Select * From VisualBasicQuizBank " + _
"Where ID = " + (intQNbr + intCount).ToString
cmdCommand.CommandText = strSQL
mrdrReader = cmdCommand.ExecuteReader()
mrdrReader.Read()
End If
'Increment count of questions generated
mintActCount = mintActCount + 1
If mintActCount > 9 Then
mrdrReader.Close()
conConnection.Close()
conConnection.Dispose()
cmdCommand.Dispose()
Exit Sub
End If
Next
conConnection.Close()
conConnection.Dispose()
cmdCommand.Dispose()
End Sub
Private Sub GradeExam()
'Grade the Exam and Update Student File
Dim strStudentAnswers, strAnswer As String
Dim intCount As Integer = 1
Dim intNoRight, intNotAnswered As Integer
'Get the right answers from data source
Dim strSQL As String = "Select Answer From " + _
"VisualBasicQuizBank Order by ID"
Dim conConnection As New _
OleDb.OleDbConnection(ConfigurationSettings.AppSettings("DataConnection"))
Dim cmdCommand As New OleDb.OleDbCommand(strSQL, conConnection)
conConnection.Open()
mrdrReader = cmdCommand.ExecuteReader()
'Grade Student answers
While mrdrReader.Read()
strAnswer = Right(Request.Form("A" & CStr(intCount)), 1)
If mrdrReader("Answer") = strAnswer Then
intNoRight += 1
End If
If strAnswer = "" Then
strAnswer = "0"
intNotAnswered += 1
End If
strStudentAnswers += strAnswer
intCount += 1
End While
mrdrReader.Close()
'Update student record to include student answers and exam status
strSQL = "Update StudentInformation SET AppearedForExam = True, NumericGrade = " + _
intNoRight.ToString + ", StudentAnswers = '" + _
strStudentAnswers + "' Where StudentID = '" + txtPassword.Text + "'"
cmdCommand.CommandText = strSQL
cmdCommand.ExecuteNonQuery()
mrdrReader.Close()
conConnection.Close()
conConnection.Dispose()
cmdCommand.Dispose()
'Call subprogram to write results
Call WriteResults(intNoRight, intCount - 1, intNotAnswered)
End Sub
Private Sub WriteResults(ByVal intNumRight As Integer, ByVal intCnt As Integer, ByVal intNA As Integer)
'Display results of grading to student
Response.Write("<h2><center><u>Here are your Results</u></h2>")
Response.Write("<h3>Name: " + txtLastName.Text + " ID: " + txtPassword.Text + "<p>")
Response.Write("Number of Correct Answers: " + intNumRight.ToString + "<br>")
Response.Write("Number of Wrong Answers: " + (intCnt - (intNA + intNumRight)).ToString + "<br>")
Response.Write("Number of Unanswered Questions: " + intNA.ToString + "<p>")
Response.Write("Your Numeric Grade is " + FormatPercent(intNumRight / 10) + "</center></h3>")
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -