簡體   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