<p>I have a dropdownlist with the autopostback set to true. I want the
user to confirm if they really want to change the value,
which on post back fires a server side event (selectedindexchanged).</p>

<p>I have tried adding an onchange attribute "return confirm('Please click OK to change. Otherwise click CANCEL?';") but it will not postback regardless of the confirm
result and the value in the list does not revert back if cancel
selected. </p>

<p>When I remove the onchange attribute from the DropdownList tag, the page does postback. It does not when the onchange attribute is added. Do I still need to wire the event handler (I'm on C# .Net 2.0 ).</p>

<p>Any leads will be helpful.</p>

<p>Thanks!</p>

[{'answer_id': 73860, 'author': 'Brian Liang', 'author_id': 5853, 'author_profile': 'https://Stackoverflow.com/users/5853', 'pm_score': 0, 'selected': False, 'text': '<p>Make sure your event is wired:</p>\n\n<pre><code>dropDown.SelectedIndexChanged += new EventHandler(dropDown_SelectedIndexChanged);\n</code></pre>\n\n<p>You can also apply a client-side attribute to return the confirmation. Set the index accordingly if cancelled.</p>\n\n<pre><code>dropDown.Attributes.Add("onchange", "javascript: return confirm(\'confirmation msg\')");\n</code></pre>\n'}, {'answer_id': 74027, 'author': 'Craig', 'author_id': 2047, 'author_profile': 'https://Stackoverflow.com/users/2047', 'pm_score': 3, 'selected': False, 'text': '<p>You can utilize the the CustomValidator control to "validate" dropdown by calling a javascript function in which you do the confirm():</p>\n\n<pre><code>        &lt;asp:DropDownList ID="TestDropDown" runat="server" AutoPostBack="true" CausesValidation="true"\n            ValidationGroup="Group1"\n            OnSelectedIndexChanged="TestDropDown_SelectedIndexChanged"&gt;\n            &lt;asp:ListItem Value="1" Text="One" /&gt;\n            &lt;asp:ListItem Value="2" Text="Two" /&gt;\n        &lt;/asp:DropDownList&gt;\n       &lt;script type="text/javascript"&gt;\n            function ConfirmDropDownValueChange(source, arguments) {\n                arguments.IsValid = confirm("Are you sure?");\n            }\n        &lt;/script&gt;\n        &lt;asp:CustomValidator ID="ConfirmDropDownValidator" runat="server"\n            ClientValidationFunction="ConfirmDropDownValueChange" Display="Dynamic" ValidationGroup="Group1"  /&gt;\n</code></pre>\n'}, {'answer_id': 74251, 'author': 'Kyle B.', 'author_id': 6158, 'author_profile': 'https://Stackoverflow.com/users/6158', 'pm_score': 4, 'selected': True, 'text': '<p>Have you tried to set the onChange event to a javascript function and then inside the function display the javascript alert and utilize the __doPostback function if it passes?</p>\n\n<p>i.e.</p>\n\n<pre><code>   \ndrpControl.Attributes("onChange") = "DisplayConfirmation();"\n\nfunction DisplayConfirmation() {\n  if (confirm(\'Are you sure you want to do this?\')) {\n    __doPostback(\'drpControl\',\'\');\n  }\n}\n</code></pre>\n'}, {'answer_id': 74289, 'author': 'Billy Jo', 'author_id': 3447, 'author_profile': 'https://Stackoverflow.com/users/3447', 'pm_score': 1, 'selected': False, 'text': "<p>Currently, you're always returning the result of the <code>confirm()</code>, so even if it returns <code>true</code>, you'll still stop execution of the event before the postback can fire. Your <code>onchange</code> should <code>return false;</code> only when the <code>confirm()</code> does, too, like this:</p>\n\n<pre><code>if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false;\n</code></pre>\n"}, {'answer_id': 74444, 'author': 'Craig', 'author_id': 2047, 'author_profile': 'https://Stackoverflow.com/users/2047', 'pm_score': 1, 'selected': False, 'text': '<p>Overriding the onchange attribute will not work if you have have AutoPostBack set to true because ASP.NET will always append the following to the end of your onchange script:</p>\n\n<pre><code>;setTimeout(\'__doPostBack(\\\'YourDropDown\\\',\\\'\\\')\', 0)\n</code></pre>\n\n<p>If you set AutoPostBack to false, then overriding onchange with a "confirm and __doPostBack"  type script (see above, err.. below) will work but you may have to manually create the __doPostBack function.</p>\n'}, {'answer_id': 2695583, 'author': 'JCallico', 'author_id': 143195, 'author_profile': 'https://Stackoverflow.com/users/143195', 'pm_score': 3, 'selected': False, 'text': '<p>The following works when the DropDownList is triggering partial postbacks:</p>\n\n<pre><code>// caching selected value at the time the control is clicked\nMyDropDownList.Attributes.Add(\n    "onclick",\n    "this.currentvalue = this.value;");\n\n// if the user chooses not to continue then restoring cached value and aborting by returning false\nMyDropDownList.Attributes.Add(\n    "onchange",\n    "if (!confirm(\'Do you want to continue?\')) {this.value = this.currentvalue; return false};");\n</code></pre>\n'}, {'answer_id': 3301127, 'author': 'NealB', 'author_id': 41114, 'author_profile': 'https://Stackoverflow.com/users/41114', 'pm_score': 1, 'selected': False, 'text': "<pre><code>if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false;\n</code></pre>\n\n<p>Always returns so dropdownlist's OnSelectedIndexChanged event fires whether user clicks OK or CANCEL.</p>\n"}, {'answer_id': 16061689, 'author': 'shailendra sharma', 'author_id': 2290912, 'author_profile': 'https://Stackoverflow.com/users/2290912', 'pm_score': 0, 'selected': False, 'text': '<pre><code>&amp;lt;asp:DropDownList runat="server" ID="ddlShailendra"  AutoPostBack="True" OnSelectedIndexChanged="ddlShailendra_SelectedIndexChanged" onchange="javascript: { if(confirm(\'Click ok to prevent post back, Cancel to make a postback\'))return true;} " &amp;gt;\n          &amp;lt;asp:ListItem Text="tes" Value="1" &gt;&amp;lt;/asp:ListItem&amp;gt;\n            &amp;lt;asp:ListItem Text="test" Value="-1"&amp;gt;&amp;lt;/asp:ListItem&amp;gt;\n            &amp;lt;/asp:DropDownList&amp;gt;\n</code></pre>\n\n<p>Write the function inline and dont have a "return" for the condition in which you want a post back. This works and is as per the standards.</p>\n'}]
['https://Stackoverflow.com/questions/73748', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/262613/']