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

时间:2013-05-27 13:04:22

标签: c# .net

我有3个表:Section,Class和Student ...现在我想根据选择类在下拉列表中加载studentName,并在下拉列表中选择部分[此处部分字段,类字段将按下拉列表选择并且StudentName也显示在下拉列表中

这是我的表预览

SectionEnty

Id,SectionTitle,Capacity

ClassEntry

Id,ClassTitle,SectionId

StudentInfo

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

1 个答案:

答案 0 :(得分: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();
        }
    }
}

现在,您的studentnamedowndown包含所选课程和部分的列表。

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