繁体   English   中英

如何从另一个下拉列表填充一个下拉列表

[英]How to populate a drop down list from another drop down list

我正在使用C#,Visual Studio 2012

我有两个下拉列表。

在第一个下拉列表中,显示学生姓名列表(来自[students]表).....在第二个下拉列表中,其显示专业(从表格[courses]中)。

这两个将来自包含两个表(学生和课程)的数据库。

假设当用户从第一个下拉列表中选择该学生时,在另一个下拉列表中它应显示与该学生相关的专业。

如何使用asp.net Web应用程序而不进行编程。

谢谢。

我认为您的意思是说没有背后的代码,而不是没有编程

您可以使用SQL数据源。

在此处输入图片说明

学生桌

StudentId | StudentName
----------+-------------
     1    |  Jon
     2    |  Marry

课程

CourseId | CourseName | StudentId
---------+------------+----------
     1   |   Physic   |     1
     2   |   Math     |     1
     3   |   Physic   |     2
     4   |   English  |     2

ASPX

<asp:DropDownList ID="DropDownListStudent" runat="server"
    DataSourceID="StudentSqlDataSource"
    DataTextField="Name" DataValueField="StudentId" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="StudentSqlDataSource" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT StudentId, StudentName FROM [Student]">
</asp:SqlDataSource>

<asp:DropDownList ID="DropDownListCourse" runat="server"
    DataSourceID="CourseSqlDataSource" DataTextField="CourseName" 
    DataValueField="CourseId">
</asp:DropDownList>
<asp:SqlDataSource ID="CourseSqlDataSource" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT * FROM [Course] WHERE StudentId=@StudentId">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownListStudent" 
            PropertyName="SelectedValue"
            Name="StudentId" Type="Int32" DefaultValue="1" />
    </SelectParameters>
</asp:SqlDataSource>

注意:确保DropDownListStudent的AutoPostBack =“ True”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM