在vb.net中随机化mcq?

时间:2014-04-16 15:06:07

标签: asp.net vb.net

我需要生成随机问答。

这是aspx代码

 <asp:Label ID="lblQuestion" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:RadioButton ID="RadioButton1" runat="server" />
    <br />
    <asp:RadioButton ID="RadioButton2" runat="server" />
    <br />
    <asp:RadioButton ID="RadioButton3" runat="server" />
    <br />
    <asp:RadioButton ID="RadioButton4" runat="server" />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" />

这是vb代码

Public Property Counter() As Integer
    Get
        Return IIf(ViewState("counter") Is Nothing, 1, CInt(ViewState("counter")))
    End Get
    Set(ByVal value As Integer)
        ViewState("counter") = value
    End Set
End Property
Sub question()
    conn.Open()
    Dim cmd As New SqlCommand("Select * From questionbank Where id=@Idquestion", conn)
    cmd.Parameters.AddWithValue("@Idquestion", Counter)
    Dim dr1 As SqlDataReader
    dr1 = cmd.ExecuteReader
    Try

        While dr1.Read()
            Me.lblSoalan.Text = dr1("question")
            Me.RadioButton1.Text = dr1("rightanswer")
            Me.RadioButton2.Text = dr1("wrong1")
            Me.RadioButton3.Text = dr1("wrong2")
            Me.RadioButton4.Text = dr1("wrong3")

        End While
    Catch ex As Exception
        MsgBox("Error", MsgBoxStyle.Exclamation, "Error")
    Finally
        conn.Close()
    End Try
End Sub

我有一套问题。用户需要单击下一步按钮才能转到另一个问题。每当用户尝试回答同一组问题时,我希望系统随机化问题

1 个答案:

答案 0 :(得分:0)

在执行SELECT语句之前,您需要随机化问题的ID。在随机值时替换Counter。使用CInt(Math.Ceiling(Rnd() * n))获取0到N(包括)

之间的随机整数值
cmd.Parameters.AddWithValue("@Idquestion", CInt(Math.Ceiling(Rnd() * n)))