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-27 13:36:50
Ted
你能不能更具體一點嗎? ! – Ted
Hei Ted ..其實我有三個dropdownlist,如classdropdown,sectiondropdown和Studentname dropdown.Now我想從StudentTable中檢索學生姓名,根據從下拉列表中選擇班級和部分... – shhuvo