简体   繁体   English

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

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

I'm working on C#, Visual studio 2012 我正在使用C#,Visual Studio 2012

I have Two Dropdownlists. 我有两个下拉列表。

In first dropdownlist it shows list of the name of students (from the table [students])..... in second dropdownlist it shows majors (from the table [courses]). 在第一个下拉列表中,显示学生姓名列表(来自[students]表).....在第二个下拉列表中,其显示专业(从表格[courses]中)。

These two will comes from database which contains two tables ( students and courses ). 这两个将来自包含两个表(学生和课程)的数据库。

Suppose when the user select the student from 1st dropdownlist so,in the other dropdownlist it should show related major to that student. 假设当用户从第一个下拉列表中选择该学生时,在另一个下拉列表中它应显示与该学生相关的专业。

How to do this using asp.net web applications without programming. 如何使用asp.net Web应用程序而不进行编程。

Thanks. 谢谢。

I think you meant to say no code behind instead of without programming . 我认为您的意思是说没有背后的代码,而不是没有编程

You can use SQL DataSources. 您可以使用SQL数据源。

在此处输入图片说明

Student Table 学生桌

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

Course 课程

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

ASPX 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>

Note: Make sure that AutoPostBack="True" for DropDownListStudent 注意:确保DropDownListStudent的AutoPostBack =“ True”

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

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