动态添加的列表框事件未触发 [英] Dynamically added listbox event is not firing

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

问题描述

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]));


        }

推荐答案

如果控件是动态创建的,则不应检查IsPostBack.
You should not check IsPostBack if control dynamically created.




检查此链接以解决您的问题.

单击此处以查看建议.

我的看法是,如果您使用的是Jquery,则在jquery中创建列表框.

然后使用jquery将数据随时发布到服务器端.


希望您知道列表框只不过是选择标记.

在jquery中处理这种情况比在asp.net中创建控件要容易

我希望你能理解我说的话.

当然可以在上面的链接中找到解决方案.

一切顺利.
Hi,

Check this link for your problem.

click here to see suggestions.

And my opinion is If you are using Jquery then create listbox in jquery .

then using jquery post that data to server side whenever you like .


I hope you know that listbox is nothing but select tag .

this type of situations are dealing in jquery is easy than creating controls in asp.net

I hope you understand what i said.

Sure you can find solution in above link.

All the Best.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆