Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that you are loading the data each time and this is resetting the selected index. Imagine this is your dropdown:</p> <pre><code>zero [selected] one two </code></pre> <p>Then in the client you change the selected index:</p> <pre><code>zero one [selected] two </code></pre> <p>This populates the hidden input <code>__EVENTARGUMENT</code> with your new index (1) and the hidden input <code>__EVENTTARGET</code> with the <code>id</code> of your dropdown. Now the server-side code kicks in and reloads your data:</p> <pre><code>zero [selected] one two </code></pre> <p>"zero" is the selected value because that is the default when the data is loaded. Then ASP.NET looks for <code>__EVENTTARGET</code> and <code>__EVENTARGUMENT</code> in the <code>Request</code> and finds your dropdown's <code>id</code> and finds the new index (1). Now your dropdown looks like this:</p> <pre><code>zero one [selected] two </code></pre> <p>Since the index has changed, the dropdown raises its <code>SelectedIndexChanged</code> event indicating that the index has changed. Obviously this is the part that is working, now lets see why selecting the first item in the list does not raise the event.</p> <p>Now lets say that we still have the dropdown in the state it was just in (with "one" being selected and the selected index of 1). What happens when we select the first item in the list on the client?</p> <p><code>__EVENTTARGET</code> and <code>__EVENTARGUMENT</code> are populated with the <code>id</code> of the dropdown and the new index (0). Then the server loads the data into the dropdown and the dropdown now looks like this again:</p> <pre><code>zero [selected] one two </code></pre> <p>Notice that since you reloaded the data <em>before</em> the events fired the index is already set to 0 because that is the default. Now when your event fires and the dropdown's selected index is set to 0, the dropdown does not see this as a change since the selected index (as far as it knows) has not changed.</p> <p>Here is how to fix the problem:</p> <pre><code>protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (!Page.IsPostBack) { this.DropDownList1.DataTextField = "Text"; this.DropDownList1.DataValueField = "Value"; this.DropDownList1.DataSource = fillQueueDropDown(); this.DropDownList1.DataBind(); } } </code></pre> <p>What this will do is only load the data into the dropdown if the page is not a postback. This means that ViewState will maintain the data for you as well as the selected index so that when you post back the dropdown will compare the new index to the index you saw in the client.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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