<p>Simple ASP.NET application.</p>

<p>I have two drop-down controls. On the first-drop down I have a JavaScript <code>onChange</code> event. The JavaScript enables the second drop-down and removes a value from it (the value selected in the first drop-down). If they click the blank first value of the drop-down, then the second drop-down will be disabled (and the options reset).</p>

<p>I also have code in the <code>OnPreRender</code> method that will enable or disable the second drop-down based on the value of the first drop-down. This is so that the value of the first drop-down can be selected in code (loading user settings).</p>

<p>My problem is:</p>

<ol>
<li>The user selects something in the first drop-down. The second drop-down will become enabled through JavaScript. </li>
<li>They then change a third drop-down that initiates a post back. After the post back the drop-downs are in the correct state (first value selected, second drop-down enabled).</li>
<li>If they then click the back button, the second drop-down will no longer be enabled although it should be since there's something selected in the first drop-down.</li>
</ol>

<p>I've tried adding a startup script (that will set the correct state of the second-drop down) through <code>ClientScript.RegisterStartupScript</code>, however when this gets called the first drop-down has a <code>selectedIndex</code> of <code>0</code>, not what it actually is. My guess is that the value of the selection gets set after my start script (but still doesn't call the <code>onChange</code> script).</p>

<p>Any ideas on what to try?</p>

[{'answer_id': 9343, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 2, 'selected': False, 'text': "<p>If the second dropdown is initially enabled through javascript (I'm assuming this is during a javascript onchange, since you didn't specify), then clicking the back button to reload the previous postback will never enable it. </p>\n\n<p>Mixing ASP.NET with classic javascript can be hairy. You might want to have a look at ASP.NET's Ajax implementation (or the third-party AjaxPanel control if you're forced to use an older ASP.NET version). Those will give you the behaviour that you want through pure C#, without forcing you to resort to javascript hackery-pokery.</p>\n"}, {'answer_id': 9365, 'author': 'Adhip Gupta', 'author_id': 384, 'author_profile': 'https://Stackoverflow.com/users/384', 'pm_score': 3, 'selected': True, 'text': '<pre><code>&lt;%@ Page Language="C#" %&gt;\n\n&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;\n\n&lt;script runat="server"&gt;\n    protected void indexChanged(object sender, EventArgs e)\n    {\n        Label1.Text = " I did something! ";\n    }\n&lt;/script&gt;\n\n&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;\n&lt;head runat="server"&gt;\n    &lt;title&gt;Test Page&lt;/title&gt;\n&lt;/head&gt;\n&lt;body&gt;\n    &lt;script type="text/javascript"&gt;\n        function firstChanged() {\n            if(document.getElementById("firstSelect").selectedIndex != 0)\n                document.getElementById("secondSelect").disabled = false;\n            else\n                document.getElementById("secondSelect").disabled = true;\n        }\n    &lt;/script&gt;\n    &lt;form id="form1" runat="server"&gt;\n    &lt;div&gt;\n        &lt;select id="firstSelect" onchange="firstChanged()"&gt;\n            &lt;option value="0"&gt;&lt;/option&gt;\n            &lt;option value="1"&gt;One&lt;/option&gt;\n            &lt;option value="2"&gt;Two&lt;/option&gt;\n            &lt;option value="3"&gt;Three&lt;/option&gt;\n        &lt;/select&gt;\n        &lt;select id="secondSelect" disabled="disabled"&gt;\n            &lt;option value="1"&gt;One&lt;/option&gt;\n            &lt;option value="2"&gt;Two&lt;/option&gt;\n            &lt;option value="3"&gt;Three&lt;/option&gt;\n        &lt;/select&gt;\n        &lt;asp:DropDownList ID="DropDownList1" AutoPostBack="true" OnSelectedIndexChanged="indexChanged" runat="server"&gt;\n            &lt;asp:ListItem Text="One" Value="1"&gt;&lt;/asp:ListItem&gt;\n            &lt;asp:ListItem Text="Two" Value="2"&gt;&lt;/asp:ListItem&gt;    \n        &lt;/asp:DropDownList&gt;\n        &lt;asp:Label ID="Label1" runat="server"&gt;&lt;/asp:Label&gt;\n    &lt;/div&gt;\n    &lt;/form&gt;\n    &lt;script type="text/javascript"&gt;\n        window.onload = function() {firstChanged();}\n    &lt;/script&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>Edit: Replaced the whole code. This should work even in your user control.\nI believe that Register.ClientScriptBlock is not working because the code you write in that block is executed <em>before</em> window.onload is called. And, I assume (I am not sure of this point) that the DOM objects do not have their values set at that time. And, this is why you are getting selectedIndex as always 0.</p>\n'}]
['https://Stackoverflow.com/questions/9341', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/233/']