下拉列表选择索引变化的相关问题

本文关键字:问题 变化 选择 索引 下拉列表 | 更新日期: 2023-09-27 18:15:37

我有3个表:Section, class和Student…现在我想在下拉列表中加载基于选择类的下拉列表中的选择部分的studentName[在这里,section字段,Class字段将被下拉列表选中,并且studentName也显示在下拉列表中]

这是我的表预览

SectionEnty

Id,SectionTitle,Capacity

ClassEntry

Id,ClassTitle,SectionId

StudentInfo

Id,FullName,ClassId,Section

这对我很有帮助。如果有人在这种情况下帮助我,因为我已经准备好了,但它不工作,因为多个索引处理。

下拉列表选择索引变化的相关问题

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();
        }
    }
}

现在,您的studentname下拉列表包含了所选的类和节的列表。