我有 3 个下拉列表,我想绑定并检索它们的数据……在这 3 个下拉列表中,第一个是绑定 onload 事件。问题是,当我在第一个下拉列表中进行选择时,第二个下拉列表填充了适当的数据,但是当我选择第二个下拉列表中的项目时,它只会引用,不起作用..我想选择任何在此但我无法编码的项目如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Clear();
MySqlCommand cmd = new MySqlCommand();
//MySqlDataAdapter da;
MySqlDataReader dr;
DataSet ds = new DataSet();
cmd.Connection = con;
cmd.CommandText = "retrieve_course";
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
//da = new MySqlDataAdapter(cmd);
//cmd.ExecuteNonQuery();
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(new ListItem(dr.GetString("course_name").ToString(), dr.GetInt64("course_id").ToString()));
}
Page.DataBind();
con.Close();
//teacher dropdown
cmd.Connection = con;
cmd.CommandText = "retrieve_teacher";
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
//da = new MySqlDataAdapter(cmd);
//cmd.ExecuteNonQuery();
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList4.Items.Add(new ListItem(dr.GetString("fname").ToString() + dr.GetString("fname").ToString(), dr.GetString("teacher_id").ToString()));
}
Page.DataBind();
con.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
// Response.Write("helloo");
MySqlCommand cmd = new MySqlCommand();
//MySqlDataAdapter da;
MySqlDataReader dr;
DataSet ds = new DataSet();
cmd.Connection = con;
cmd.CommandText = "retrieve_semester";
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
Int64 param1 = Convert.ToInt64(DropDownList1.SelectedItem.Value.ToString());
cmd.Parameters.AddWithValue("param1", param1);
//da = new MySqlDataAdapter(cmd);
//cmd.ExecuteNonQuery();
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList2.Items.Add(new ListItem(dr.GetString("sem_name").ToString(), dr.GetInt64("sem_id").ToString()));
}
// Page.DataBind();
con.Close();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList3.Items.Clear();
// Response.Write("helloo");
MySqlCommand cmd = new MySqlCommand();
//MySqlDataAdapter da;
MySqlDataReader dr;
DataSet ds = new DataSet();
cmd.Connection = con;
cmd.CommandText = "retrieve_subject";
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
Int64 param1 = Convert.ToInt64(DropDownList1.SelectedItem.Value.ToString());
Int64 param2 = Convert.ToInt64(DropDownList2.SelectedItem.Value.ToString());
cmd.Parameters.AddWithValue("param1", param1);
cmd.Parameters.AddWithValue("param2", param2);
//da = new MySqlDataAdapter(cmd);
//cmd.ExecuteNonQuery();
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList3.Items.Add(new ListItem(dr.GetString("sub_title").ToString(), dr.GetInt64("sub_id").ToString()));
}
// Page.DataBind();
con.Close();
}
aspx 页面:----
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage3.master" AutoEventWireup="true" CodeFile="retrive.aspx.cs" Inherits="retrive" %>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>select</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList2_SelectedIndexChanged"
AppendDataBoundItems="True">
<asp:ListItem>select</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True" >
</asp:DropDownList>
<asp:DropDownList ID="DropDownList4" runat="server">
</asp:DropDownList>