Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically added ListBox's EventHandler is not firing on ASP.Net
    primarykey
    data
    text
    <p>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?</p> <p><strong>EDIT:</strong> I tried to add </p> <pre><code>li.Attributes.Add("onselectedindexchanged","selectedIndexChanged"); </code></pre> <p>or</p> <pre><code>li.AutoPostBack = true; </code></pre> <p>but still not firing...</p> <pre><code>private List&lt;string&gt; myList = new List&lt;string&gt;() { "Serkan", "Kadir" }; private List&lt;string&gt; mySecondList = new List&lt;string&gt;() { "Istanbul", "Ankara" }; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (myList.Count &gt; 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload