1

我随机将 3 个问题显示在 asp.net 页面上。我想将安全问题与数据库中的正确答案相匹配。但是当我在文本框中输入正确答案并单击“下一步”按钮后,答案与屏幕上显示的当前问题不匹配。但是,如果我单击“下一步”按钮,显示的下一个问题与我在上一个问题的文本框中输入的答案相匹配。我认为这是因为刷新页面后答案与问题和答案相匹配。请帮忙。这是我的代码。谢谢

vb.net代码

Dim SecurityQuestion As New DBDataContext
Dim randomNumber As Integer

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim SecurityQuestion As New DBDataContext
    Dim rowCount As Integer = (From t In SecurityQuestion.tblSecurityQuestions Select t).Count + 1
End Sub

Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
    ValidateAnswer()
End Sub

Private Sub LoadSecurityQuestion()
    Dim SecurityQuestion As New DBDataContext
    Dim mySecurityQuestion = (From c In SecurityQuestion.tblSecurityQuestions Where c.PkID = randomNumber Select c)
    Dim rowCount As Integer = (From t In SecurityQuestion.tblSecurityQuestions Select t).Count + 1
    randomNumber = New Random().Next(1, rowCount)
    With gvQuestion
        .DataSource = mySecurityQuestion
        .DataBind()
    End With
End Sub

Private Sub ValidateAnswer()
    Dim SecurityQuestion As New DBDataContext
    Dim validate = (From r In SecurityQuestion.tblRegistrations From s In SecurityQuestion.tblSecurityQuestions _
        Where s.PkID = r.Q01 And r.A01 = txtSecurityAns.Text And r.UserID = ad And s.PkID = randomNumber _
        Select r)

    If validate.Count > 0 Then
        Msg3.Text = "Correct Answer"
    Else
        Msg3.Text = "Invalid Answer"
    End If
End Sub
4

1 回答 1

0

在我发布此答案时,您的代码似乎可能丢失了一小部分。例如,我看不到您实际调用方法 LoadSecurityQuestion 的位置,但听起来您可能会在验证成功后重新调用方法 LoadSecurityQuestion。

认为您可以通过使用“if”限定符包装对 LoadSecurityQuestion 的调用来解决此问题。例如,假设您在 Load Event 处理程序中调用 LoadSecurityQuestion。如果您这样做了,您的新 Load 事件处理程序可能会显示如下内容:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    ...
    If IsPostBack Then
        ' If you wrap all of your Me.LoadSecurityQuestion calls 
        ' with "If" statements like this one, then it should keep
        ' the security question from changing when the user 
        ' enters the correct answer
        If Msg3.Text <> "Correct Answer" Then                
            Me.LoadSecurityQuestion()
        End If
    End If
    ...
End Sub
于 2012-09-02T03:41:06.647 回答