After capturing the SelectedIndexChanged event for a ListBox control, you find that the event handler doesn’t execute. What could the problem be ?
-
The AutoEventWireup attribute is set to False
-
The AutomaticPostBack attribute is set to False
-
The codebehind module is not properly compiled
-
The ListBox must be defined WithEvents
For ListBox SelectedIndexChanged to fire and execute its event handler, AutoPostBack must be set to True. When True, the control automatically posts back to the server on selection change, triggering the event. If False (the default), the selection changes client-side but no postback occurs, so the server-side event handler never runs. AutoEventWireup relates to event binding method, and WithEvents is a VB.NET concept not used here. The answer identifies the correct property, though it misspells it as 'AutomaticPostBack' instead of 'AutoPostBack'.
The correct answer is that AutoPostBack is set to False. A ListBox's SelectedIndexChanged event only fires immediately (with a round trip to the server) if AutoPostBack is True; if it's False, the selection change is recorded but the event handler won't execute until some other control triggers a postback. AutoEventWireup controls whether ASP.NET automatically wires page lifecycle events (like Page_Load) by naming convention — unrelated to control-level events. A broken codebehind compile would cause a build error, not silent non-execution. WithEvents is VB syntax for handling events via the Handles clause, but ASP.NET Web Forms event handlers are typically wired via the designer/AutoEventWireup mechanism, not a hard requirement here.