user3236918
user3236918

Reputation: 47

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.

Upvotes: 0

Views: 761

Answers (1)

Win
Win

Reputation: 62260

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

You can use SQL DataSources.

enter image description here

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

Upvotes: 4

Related Questions