Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Change events are raised on every postback for which they're relevant - as described in the MSDN topic "<a href="http://msdn.microsoft.com/en-us/library/y3bwdsh3.aspx" rel="nofollow noreferrer">ASP.NET Web Server Control Event Model</a>."</p> <blockquote> <p>Change events in HTML server controls and Web server controls, such as the TextBox control, do not immediately cause a post. Instead, <strong>they are raised</strong> <strong>the next time a post occurs</strong>.</p> </blockquote> <p>When users click your 'Edit Artist' button, ASP.NET thinks <code>lbLinks.SelectedIndex</code> has changed, so it invokes its <code>SelectedIndexChanged</code> handler.</p> <p>The <em>reason</em> ASP.NET thinks the index has changed is this: when the page first loads, <code>lbLinks</code> doesn't have a selected index (or value) unless you say otherwise by explicitly setting it. Until you do that, the selected index is -1 and its selected value is an empty string. The selected value (in this case, an empty string) is written to view state when the page is rendered <em>so that ASP.NET can tell if the value has changed on postbacks</em>.</p> <p>You can observe this while debugging by inspecting your list boxes' selected indices and values before rendering, or you can use one of the online view state decoders (<a href="http://lachlankeown.blogspot.com/2008/05/online-viewstate-viewer-decoder.html" rel="nofollow noreferrer">like this one</a>) to see what's in your page when it's first written (though to read this, you need to know about <a href="http://msdn.microsoft.com/en-us/library/ms972976.aspx#viewstate_topic11" rel="nofollow noreferrer">the structure of serialized view state data</a>).</p> <p>When you next post back, the HTML <code>&lt;select&gt;</code> element <code>lbLinks</code> <em>has a non-empty value</em>, and it is submitted as part of the post data. Take a look at <code>Request.Form["lbLinks"]</code> and you'll see that it equals <code>lbLinks.Items[0].Value</code>.</p> <p>ASP.NET maps the posted value to <code>lbLinks.SelectedValue</code>, but it also knows that the selected value <em>used to be an empty string</em> - it gets the old value from view state. Since the two values are different, the process raises the control's selected index changed event, causing the undesirable behavior you've observed.</p> <p><a href="https://stackoverflow.com/questions/1733077/why-do-unrelated-autopostback-methods-get-called-when-an-asp-net-listbox-autopo/1752797#1752797">As boon suggested</a>, the solution is to always explicitly set the <code>SelectedIndex</code> for all your <code>ListBox</code> controls when you're using the <code>OnSelectedIndexChanged</code> event, even if you're just setting the index to zero.</p> <p>(The AutoPostBack setting is an unrelated red herring. If you remove it from both list boxes, their <code>OnSelectedIndexChanged</code> events will both fire every time you click the button.)</p>
 

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