-1

我有 3 个表:Section、Class 和 Student...现在我想根据选择 Class 在下拉列表中加载 studentName,并在下拉列表中选择部分进入下拉列表]

这是我的表格预览

部分实体

Id,SectionTitle,Capacity

班级入口

Id,ClassTitle,SectionId

学生信息

Id,FullName,ClassId,Section

这对我很有帮助。如果有人在这种情况下帮助我,因为我已经准备好尝试但由于多个索引处理而无法正常工作。

4

1 回答 1

0

ASPX:

<asp:DropDownList ID="classdropdown" runat="server" OnSelectedIndexChanged="classdropdown_SelectedIndexChanged" AutoPostBack="true" />

<asp:DropDownList ID="sectiondropdown" runat="server" OnSelectedIndexChanged="sectiondropdown_SelectedIndexChanged" AutoPostBack="true" />

<asp:DropDownList ID="studentnamedropdown" runat="server" />

C#:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.LoadClassDropdown();
    }
}

// set initial values
private void LoadClassDropdown()
{
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT Id,SectionTitle FROM SectionEnty", "your connection string"))
    {    
        using (DataSet ds = new DataSet())
        {
            da.SelectCommand.Connection.Open();
            da.Fill(ds);
            da.SelectCommand.Connection.Close();

            classdropdown.DataSource = ds;
            classdropdown.DataValueField = "Id";
            classdropdown.DataTextField = "SectionTitle";
            classdropdown.DataBind();
        }
    }
}

protected void classdropdown_SelectedIndexChanged(object sender, EventArgs e)
{
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT Id,ClassTitle FROM ClassEntry WHERE SectionId = @SectionId", "your connection string")
    {
        da.SelectCommand.Parameters.Add(new SqlParameter("@SectionId", sectiondropdown.SelectedValue));

        using (DataSet ds = new DataSet())
        {
            da.SelectCommand.Connection.Open();
            da.Fill(ds);
            da.SelectCommand.Connection.Close();

            sectiondropdown.DataSource = ds;
            sectiondropdown.DataValueField = "Id";
            sectiondropdown.DataTextField = "ClassTitle";
            sectiondropdown.DataBind();
        }
    }
}

protected void sectiondropdown_SelectedIndexChanged(object sender, EventArgs e)
{
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT Id,FullName FROM StudentInfo WHERE ClassId = @ClassId", "your connection string"))
    {
        da.SelectCommand.Parameters.Add(new SqlParameter("@ClassId", classdropdown.SelectedValue));

        using (DataSet ds = new DataSet())
        {
            da.SelectCommand.Connection.Open();
            da.Fill(ds);
            da.SelectCommand.Connection.Close();

            studentnamedropdown.DataSource = ds;
            studentnamedropdown.DataValueField = "Id";
            studentnamedropdown.DataTextField = "FullName";
            studentnamedropdown.DataBind();
        }
    }
}

现在你的 studentnamedropdown 包含所选班级和部分的列表。

有任何问题,请告诉我...

于 2013-05-27T13:36:50.403 回答