使用动态方法处理多个下拉列表

时间:2013-05-10 16:33:35

标签: asp.net drop-down-menu

我有3下拉lis我想绑定并检索它们的数据... 在这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>

0 个答案:

没有答案