简体   繁体   中英

How to populate a drop down list from another drop down list

I'm working on 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]).

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.

Thanks.

I think you meant to say no code behind instead of without programming .

You can use SQL DataSources.

在此处输入图片说明

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

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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