asp.netdrop-down-menuonchangeautopostback

DropdownList autoposback after client confirmation


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).

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.

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 ).

Any leads will be helpful.

Thanks!


Solution

  • 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?

    i.e.

       
    drpControl.Attributes("onChange") = "DisplayConfirmation();"
    
    function DisplayConfirmation() {
      if (confirm('Are you sure you want to do this?')) {
        __doPostback('drpControl','');
      }
    }