动态添加的ListBox的事件处理程序不开火ASP.Net [英] Dynamically added ListBox's EventHandler is not firing on ASP.Net

查看:109
本文介绍了动态添加的ListBox的事件处理程序不开火ASP.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,进出口如果我的问题有可能重复,但我找遍太多了,我不能找到解决办法遗憾。该之情况是我有一个用户控件。此用户控件将动态创建列表框,如果给定列表的计数> 0的控件动态添加,没有与他们的加入没有问题,但该事件处理程序不加入。如果我在第一个列表框中选择一个项目,该secondListBox将动态添加,并且项目将动态地添加了。首先出现列表框,但SelectedIndexChanged事件处理程序不起作用。有什么可以用我的code中的问题?

编辑:我试图添加

  li.Attributes.Add(onselectedindexchanged,的SelectedIndexChanged);

  li.AutoPostBack = TRUE;

但仍然没有射击...

 私人列表<串GT; myList中=新名单<串GT;(){塞尔坎,卡迪尔};
    私人列表<串GT; mySecondList =新的List<串GT;(){伊斯坦布尔,安卡拉};    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!Page.IsPostBack)
        {
            如果(myList.Count大于0)
            {
                李列表框=新的ListBox();
                。li.ID = Guid.NewGuid()的ToString();
                li.SelectedIndexChanged + =新的EventHandler(this.selectedIndexChanged);
                的foreach(在myList中的字符串项)
                {
                    li.Items.Add(项目);
                }                this.Controls.Add(LI);
            }
        }
    }    的SelectedIndexChanged私人无效(对象发件人,EventArgs的发送)
    {
        列表框LI2 =新的ListBox();
        。li2.ID = Guid.NewGuid()的ToString();        的foreach(在mySecondList字符串项)
        {
            li2.Items.Add(项目);
        }
        this.Controls.Add(LI2);
    }


解决方案

伙计们,我的code工作良好。我只是在一个差异视觉工作室今天想这一点,和一切工作正常。 @ RichardW1001和@Microgen感谢您的关注和评论。

First of all, Im sorry if my question has possible duplicate, but I searched too much, and I cant find the solution. The scenerio is I have a UserControl. This user control will create dynamically ListBoxes if the given List's count > 0. The controls are adding dynamically, there is no problem with adding them, but the event handler is not adding. If I select an item in first listBox, the secondListBox will be added dynamically, and items will add dynamically too. First listBox appears, but the selectedIndexChanged event handler doesn't work. What can be the problem with my code?

EDIT: I tried to add

li.Attributes.Add("onselectedindexchanged","selectedIndexChanged");

or

li.AutoPostBack = true;

but still not firing...

private List<string> myList = new List<string>() { "Serkan", "Kadir" };
    private List<string> mySecondList = new List<string>() { "Istanbul", "Ankara" };

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (myList.Count > 0)
            {
                ListBox li = new ListBox();
                li.ID = Guid.NewGuid().ToString();
                li.SelectedIndexChanged += new EventHandler(this.selectedIndexChanged);
                foreach (string item in myList)
                {
                    li.Items.Add(item);
                }

                this.Controls.Add(li);
            }
        }
    }

    private void selectedIndexChanged(object sender, EventArgs e)
    {
        ListBox li2 = new ListBox();
        li2.ID = Guid.NewGuid().ToString();

        foreach (string item in mySecondList)
        {
            li2.Items.Add(item);
        }
        this.Controls.Add(li2);
    }

解决方案

Folks, my code is working good. I just tried this in a diff visual studio today, and everything worked fine. @RichardW1001 and @Microgen thank you for your interest and posts.

这篇关于动态添加的ListBox的事件处理程序不开火ASP.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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