Asp.net 将下拉列表连接到数据库

Asp.net 将下拉列表连接到数据库,asp.net,sql,database,vb.net,drop-down-menu,Asp.net,Sql,Database,Vb.net,Drop Down Menu,一周前,我问了一个关于如何将复选框连接到数据库的问题,但我意识到,由于存在多对多问题,我的数据库无法按我希望的方式运行 因此,现在用户将选择一个“问题”,然后从下拉列表中选择新测试的“paperID” 因此,我需要能够从下拉列表中检索paperID,从所选行中检索QuestionID,并将它们保存在一个新表中 下面是我当前为“TestCreation”页面编写的代码,该页面将PaperID和“QuestionID”编译到“QuestionInPaper”表中 Public Class testc

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

因此,现在用户将选择一个“问题”,然后从下拉列表中选择新测试的“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表中呢。主要问题是检索复选框内的值


感谢您的帮助

也许你的头衔搞错了?可能是关于DropDown而不是DropBox:啊,谢谢,我会纠正它: