开发者

SelectedIndexChanged for programmatically created dropdownlist in ASP.NET fires multiple times

Consider the following:

dim dropdownlist1 as new dropdownlist
dim dropdownlist2 as new dropdownlist
dim dropdownlist3 as new dropdownlist

dropdownlist1.AutoPostBack = true
dropdownlist2.AutoPostBack = true
dropdownlist3.AutoPostBack = true

AddHandler dropdownlist1.SelectedIndexChanged, AddressOf SomeEvent
AddHandler dropdownlist2.SelectedIndexChanged, AddressOf SomeEvent
AddHandler dropdownlist3.SelectedIndexChanged, AddressOf SomeEvent

Edit: I want an event to fire no matter which dropdown is selected. Edit:

The SomeEvent fires as expected when any of the dropdown's selection is changed. However if say DropdownList2 has a selection made then I make a selection with either DropDownList1 or DropdownList3, then SomeEvent fires again. What is causing this behavior and how do I get just a single raising of that event?

I suspect that when the viewstate fo开发者_如何学JAVAr the dynamcially created dropdownlists is restored and the selection restored, then the event is fired because technically the selected index did change when the control was recreated. The reason I suspect this is that the event fires the for each dropdownlist...


The event will fire when the value of that property is changed programatically after the event is wired up. This is likely the cause of multiple calls of the function. This is why you need to add any event handlers after the viewstate is loaded. Try looking at the stack trace for each time the method is called to find where this is happening.


This is what I have and it works fine (Sorry I am a C# guy)

protected void Page_Load(object sender, EventArgs e)
    {       
            DropDownList objlist1 = new DropDownList();
            DropDownList objlist2 = new DropDownList();
            DropDownList objlist3 = new DropDownList();

            objlist1.Items.Add("aaa");
            objlist1.Items.Add("bbb");
            objlist2.Items.Add("cc");
            objlist2.Items.Add("ddd");
            objlist3.Items.Add("eee");
            objlist3.Items.Add("fff");

            objlist1.AutoPostBack = true;
            objlist2.AutoPostBack = true;
            objlist3.AutoPostBack = true;

            objlist1.SelectedIndexChanged += new EventHandler(objlist1_SelectedIndexChanged);
            objlist2.SelectedIndexChanged += new EventHandler(objlist1_SelectedIndexChanged);
            objlist3.SelectedIndexChanged += new EventHandler(objlist1_SelectedIndexChanged);

            form1.Controls.Add(objlist1);
            form1.Controls.Add(objlist2);
            form1.Controls.Add(objlist3);        
    }

    void objlist1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write("change happened");
    }

Everytime the drop down changes it writes Change happened (checked with break point and it happens only once)


Not sure if this helps, but where in the page lifecycle are you creating the controls?

I usually like to call EnsureChildControls during Page Init, so all controls are created before ViewState is loaded, and definitely before post back processing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜