views:

5308

answers:

7

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!

A: 

Make sure your event is wired:

dropDown.SelectedIndexChanged += new EventHandler(dropDown_SelectedIndexChanged);

You can also apply a client-side attribute to return the confirmation. Set the index accordingly if cancelled.

dropDown.Attributes.Add("onchange", "javascript: return confirm('confirmation msg')");
Floetic
A: 

You can utilize the the CustomValidator control to "validate" dropdown by calling a javascript function in which you do the confirm():

        <asp:DropDownList ID="TestDropDown" runat="server" AutoPostBack="true" CausesValidation="true"
            ValidationGroup="Group1"
            OnSelectedIndexChanged="TestDropDown_SelectedIndexChanged">
            <asp:ListItem Value="1" Text="One" />
            <asp:ListItem Value="2" Text="Two" />
        </asp:DropDownList>
       <script type="text/javascript">
            function ConfirmDropDownValueChange(source, arguments) {
                arguments.IsValid = confirm("Are you sure?");
            }
        </script>
        <asp:CustomValidator ID="ConfirmDropDownValidator" runat="server"
            ClientValidationFunction="ConfirmDropDownValueChange" Display="Dynamic" ValidationGroup="Group1"  />
Craig
+3  A: 

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','');
  }
}
Kyle B.
this solution worked. I liked it because it was the simplest of all. Never had thought that I would need to call __doPostBack(().Thanks!
Aamir Ghanchi
A: 

Currently, you're always returning the result of the confirm(), so even if it returns true, you'll still stop execution of the event before the postback can fire. Your onchange should return false; only when the confirm() does, too, like this:

if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false;
Bill Ayakatubby
A: 

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:

;setTimeout('__doPostBack(\'YourDropDown\',\'\')', 0)

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.

Craig
A: 

The following works when the DropDownList is triggering partial postbacks:

// caching selected value at the time the control is clicked
MyDropDownList.Attributes.Add(
    "onclick",
    "this.currentvalue = this.value;");

// if the user chooses not to continue then restoring cached value and aborting by returning false
MyDropDownList.Attributes.Add(
    "onchange",
    "if (!confirm('Do you want to continue?')) {this.value = this.currentvalue; return false};");
JCallico
A: 
if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false;

Always returns so dropdownlist's OnSelectedIndexChanged event fires whether user clicks OK or CANCEL.