ASP.Net dropdown control loses state after being set dynamically through javascript
<p>I have an asp.net dropdownlist control. There are two ways it can be selected.</p>

<p>The first is through a button that sets the value in the drop-down list via javascript.</p>

<p>The second is by manually selecting the dropdownlist option.</p>

<p>Each of these methods work by themselves.</p>

<p>If I do the first followed by the second and then hit save - the value that is saved is the value set by the javascript. The manual selection did not have any effect.</p>

<p>Does anyone know why this is the case? </p>

<p>EDIT: </p>

<p>In the HTML Head section:</p>

<pre><code>&lt;script&gt;
function Select(allocationId) {
    document.getElementById('Accounts').value=allocationId;
}
&lt;/script&gt;
</code></pre>

<p>In the HTML Body:</p>

<pre><code>&lt;asp:DropDownList ID="Accounts" runat="server"&gt;&lt;/asp:DropDownList&gt;
&lt;button onclick="Select(&lt;%#"'" + DataBinder.Eval(Container.DataItem, "Associated_AccountId")  + "'"%&gt;)" type="button"&gt;Select&lt;/button&gt;
&lt;asp:Button ID="btn_SaveAndClose" runat="server" Text="Save and Close" OnClick="btn_SaveAndClose_Click"&gt;&lt;/asp:Button&gt;
</code></pre>

<p>In the code behind:</p>

<pre><code>protected void btn_SaveAndClose_Click(object sender, System.EventArgs e)
{
   int id = Convert.ToInt32(this.Accounts.SelectedValue);
}
</code></pre>

<p>EDIT:</p>

<p>Funnily enough - when I use:</p>

<pre><code>int id =  Convert.ToInt32(Request.Form["Accounts"]);
</code></pre>

<p>it works. But I don't know why.</p>
