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!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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','');
  }
}

Method 2

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"  />

Method 3

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};");

Method 4

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;

Method 5

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.

Method 6

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.

Method 7

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')");

Method 8

&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;} " &gt;
          &lt;asp:ListItem Text="tes" Value="1" >&lt;/asp:ListItem&gt;
            &lt;asp:ListItem Text="test" Value="-1"&gt;&lt;/asp:ListItem&gt;
            &lt;/asp:DropDownList&gt;

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x