使用DetailsView的随机问题(帮助)

时间:2010-09-26 17:11:49

标签: c# .net asp.net sql-server vb.net

问候大家,我想问一下如何在我的数据库中随机检索数据行...我能够创建一个在线测验,其中显示问题,连续顺序选择但我想要什么是的,每当用户开始测验时,它将以随机顺序显示问题。我正在使用mssql 2005,因为我的数据库是我的代码..任何建议或建议都很受欢迎..谢谢你,祝你有个美好的一天..

  

QuizPage.aspx

<asp:DetailsView ID="questionDetails" runat="server" AutoGenerateRows="False" 
                CellPadding="4" ForeColor="#333333" 
                GridLines="None" Height="50px" Width="550px">
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
                <RowStyle BackColor="#F7F6F3" CssClass="generaltext" ForeColor="#333333" />
                <FieldHeaderStyle BackColor="#E9ECF1" CssClass="boldtext" Font-Bold="True" 
                    Width="80px" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <Fields>
                    <asp:TemplateField HeaderText="Question ID">           
                        <ItemTemplate>
                                       <asp:Label ID="question_id" runat="server" Text='<%# Bind("question_id") %>'></asp:Label>
                        </ItemTemplate>                                                                             
                        </asp:TemplateField> 
                         <asp:TemplateField HeaderText="Question:">           
                        <ItemTemplate>
                                       <asp:Label ID="quiz_question" runat="server" Text='<%# Bind("quiz_question") %>'></asp:Label>
                        </ItemTemplate>                                                                             
                        </asp:TemplateField> 
                         <asp:TemplateField HeaderText="Choice 1:">           
                        <ItemTemplate>
                                       <asp:Label ID="choice1" runat="server" Text='<%# Bind("choice1") %>'></asp:Label>
                        </ItemTemplate>                                                                             
                        </asp:TemplateField> 
                         <asp:TemplateField HeaderText="Choice 2:">           
                        <ItemTemplate>
                                       <asp:Label ID="choice2" runat="server" Text='<%# Bind("choice2") %>'></asp:Label>
                        </ItemTemplate>                                                                             
                        </asp:TemplateField> 
                         <asp:TemplateField HeaderText="Choice 3:">           
                        <ItemTemplate>
                                       <asp:Label ID="choice3" runat="server" Text='<%# Bind("choice3") %>'></asp:Label>
                        </ItemTemplate>                                                                             
                        </asp:TemplateField> 
                         <asp:TemplateField HeaderText="Choice 4:">           
                        <ItemTemplate>
                                       <asp:Label ID="choice4" runat="server" Text='<%# Bind("choice4") %>'></asp:Label>
                        </ItemTemplate>                                                                             
                        </asp:TemplateField>
                </Fields>
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" CssClass="generaltext" 
                    ForeColor="#284775" />
            </asp:DetailsView>

    Your Answer:&nbsp;
            <asp:DropDownList ID="answerDropDownList" runat="server" 
                style="margin-bottom: 0px">
                <asp:ListItem Value="1">Answer 1</asp:ListItem>
                <asp:ListItem Value="2">Answer 2</asp:ListItem>
                <asp:ListItem Value="3">Answer 3</asp:ListItem>
                <asp:ListItem Value="4">Answer 4</asp:ListItem>
            </asp:DropDownList>

  <asp:Button ID="buttonNext" runat="server" Text="Next" />

QuizPage.aspx.vb

Private Function CreateConnection() As SqlConnection
    Dim _connectionString As String = ConfigurationManager.ConnectionStrings("LMSConnectionString").ConnectionString
    Return New SqlConnection(_connectionString)
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        getQuestions()
    End If
End Sub
Private Sub getQuestions()
    Dim quiz_id As Integer
    quiz_id = Session("quiz_id")
    Dim connection As SqlConnection = CreateConnection()
    Dim command As SqlCommand = Nothing
    Dim dt As DataTable = New DataTable()
    command = New SqlCommand("SELECT question_id,quiz_question, choice1, choice2, choice3, choice4, answer, quiz_id FROM tblQuizQuestion WHERE (quiz_id = @quiz_id)", connection)
    command.Parameters.AddWithValue("@quiz_id", quiz_id)
    Dim ad As SqlDataAdapter = New SqlDataAdapter(command)
    ad.Fill(dt)
    questionDetails.DataSource = dt
    questionDetails.DataBind()
End Sub
Protected Sub buttonNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonNext.Click

    Try

        ' 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("answer").ToString()
        a.UserAnswer = answerDropDownList.SelectedValue.ToString()

        Dim al As ArrayList
        al = CType(Session("AnswerList"), ArrayList)
        al.Add(a)

        Session.Add("AnswerList", al)

    Catch ex As Exception


        Response.Redirect("default.aspx")
    End Try

    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

2 个答案:

答案 0 :(得分:1)

  • 不要使用SqlDataSource。
  • 在代码隐藏
  • 中加载一次行
  • shuffle List<Question>
  • 将列表存储在会话对象

答案 1 :(得分:1)

在搜索问题的解决方案时,我偶然发现了这篇文章:

http://haacked.com/archive/2004/06/21/658.aspx

通过在Select SQL语句中使用ORDER BY NEWID(),您可以在每次检索记录时随机化结果。我在SQL Server 2008上尝试过,并且可以为100多条记录提供出色的工作。所以您需要做的就是将Select SQL修改为:

SELECT question_id,quiz_question, choice1, choice2, choice3, choice4, answer, quiz_id FROM tblQuizQuestion WHERE (quiz_id = @quiz_id) ORDER BY NEWID()

另一种方法是创建一个随机化行顺序的RandomizeDataTable函数。如果上述解决方案对您不起作用,那么我们可以看一下。