动态添加的列表框事件未触发

动态添加的列表框事件未触发

本文介绍了动态添加的列表框事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My problem is when I clicked on one listbox another list box has to come dynamically with related values.... list box is coming but listbox event is not firing....I saw some answers in this website but that answer is not suited for my application can any one please give the answer... Here is a quick code please see the code and give me the answers asap...

Thanks in advance....


protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            SqlCommand com = new SqlCommand("select EmpId,EmpName from OrgChart where      ManagerID=0", con);
            con.Open();
            SqlDataReader dr = com.ExecuteReader();
           int i=0;

            while (dr.Read())
            {
                empid[i] = Convert.ToInt32(dr[0].ToString());
                ename[i] = dr[1].ToString();
                i++;


            }
            for(int j=0;j<i;j++)
                ListBox1.Items.Add(empid[j]+"-"+ename[j]);

            ListBox1.Items.Add("create new");
        }



    }


    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Table1.Visible =false;
        Table2.Visible = false;
        string comand = ListBox1.SelectedValue.ToString();
        if (comand == "create new")
        {
            Table1.Visible = true;
        }
        else
        {
            Table2.Visible = true;
            string[] emp = comand.Split('-');
            getdetails(Convert.ToInt32(emp[0]));


        }
    }
    static ListBox lb = new ListBox();
    void getdetails(int empid)
    {

            managerid = empid;

                con.Open();
            com = new SqlCommand("select EmpId,EmpName from OrgChart where ManagerID=" + managerid + "", con);
            dr = com.ExecuteReader();

            lb.ID = "lb1";
            lb.AutoPostBack = true;


            while (dr.Read())
            {
                lb.Items.Add(dr[0].ToString() + "-" + dr[1].ToString());

            }
            lb.Items.Add("create new");

            lb.SelectedIndexChanged += new EventHandler(this.lb_SelectedIndexChanged);

            this.Controls.Add(lb);



    }

    void lb_SelectedIndexChanged(object sender, EventArgs e)
    {
        Table1.Visible = false;
        Table2.Visible = false;
        getdetails(managerid);
        string comand = ((ListBox)sender).SelectedValue.ToString();
        if (comand == "create new")
        {
            Table1.Visible = true;
        }
        else
        {
            Table2.Visible = true;

            string[] emp = comand.Split('-');
            getdetails(Convert.ToInt32(emp[0]));


        }

推荐答案



这篇关于动态添加的列表框事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:47