连接下拉列表到数据库

时间:2014-01-14 11:38:27

标签: asp.net sql database vb.net drop-down-menu

一周前我问了一个关于如何将我的复选框连接到数据库的问题,但我意识到由于存在很多问题,我的数据库无法按照我想要的方式运行。

现在,用户将选择“问题”,然后从下拉列表中选择新测试将具有的“paperID”。

所以我需要能够从下拉列表中检索paperID,并从所选行中检索QuestionID,并将它们保存在新表中。

这是我目前的'TestCreation'页面的代码,它将PaperID和'QuestionID'编译到'QuestionInPaper'表中。

Public Class testcreation1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session.Item("User Type") <> "Teacher" Then
            Response.Redirect("/")
        End If

    End Sub

    Protected Sub QuestionCompiler_Click(sender As Object, e As EventArgs) Handles QuestionCompiler.Click

        Dim allCheckedRows = From row In CreateTest.Rows.Cast(Of GridViewRow)()
                     Let chkQuestion = DirectCast(row.FindControl("QuestionSelector"), CheckBox)
                     Where chkQuestion.Checked
                     Select row
        For Each checkedRow As GridViewRow In allCheckedRows

            ' implement the save function for this row '
            Dim QuestionID As Int32 = Int32.Parse(checkedRow.Cells(0).Text)

            ' ... '
        Next

    End Sub

End Class

源代码:

    <h1>Compile Your Tests</h1>

<asp:GridView ID="CreateTest" runat="server" 
        AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
        DataKeyNames="QuestionID" DataSourceID="SqlDataSource1" ForeColor="#333333" 
        GridLines="None">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
                <asp:CheckBox ID="QuestionSelector" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="QuestionID" HeaderText="QuestionID" 
            InsertVisible="False" ReadOnly="True" SortExpression="QuestionID" />
        <asp:BoundField DataField="Answer" HeaderText="Answer" 
            SortExpression="Answer" />
        <asp:BoundField DataField="Question" HeaderText="Question" 
            SortExpression="Question" />
        <asp:BoundField DataField="SubjectID" HeaderText="SubjectID" 
            SortExpression="SubjectID" />
        <asp:TemplateField></asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
        SelectCommand="SELECT [QuestionID], [Answer], [Question], [SubjectID] FROM [Question]">
    </asp:SqlDataSource>
    <h5>Choose A Paper ID to compile your questions into</h5>
    <asp:DropDownList ID="DropDownList1" runat="server" 
        DataSourceID="SqlDataSource3" DataTextField="PaperID" DataValueField="PaperID">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
        SelectCommand="SELECT [PaperID] FROM [ExamPaper]"></asp:SqlDataSource>


    <asp:Button ID="QuestionCompiler" runat="server" Text="Compile Selected Questions" />

<h1>Preview Previous Tests</h1>

</asp:Content>

因此,您可以从后面的代码中看到我已经帮助查找数据行,但是我如何对复选框执行相同操作以将QuestionID和PaperID插入到QuestionInPaper表中我的数据库。 (主要问题是检索复选框内的值。

感谢任何帮助!

0 个答案:

没有答案