DropdownList autoposback after client confirmation

I have a drop down list with auto repeat set to true. I want the user to confirm whether they really want to change the value that, when called back again, raises an event on the server side (selectedindexchanged).

I tried adding the onchange attribute "return confirm" ("Please click" OK "to change. Otherwise, click" CANCEL? ";"), But it will not return the result regardless of the confirmation, and the value in the list does not return if cancel is selected.

When I remove the onchange attribute from the DropdownList tag, the page postbacks. This does not happen when the onchange attribute is added. I still need to hook up an event handler (I'm in C # .Net 2.0).

Any hints would be helpful.

Thank!

+3
source share
8 answers

Have you tried setting the onChange event to a javascript function and then displaying a javascript warning inside the function and using 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','');
  }
}
+8
source

You can use the CustomValidator control for the validate drop-down list by calling the javascript function in which you execute confirmation ():

        <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"  />
+6
source

, DropDownList :

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

confirm(), , true, , . onchange return false; , confirm() :

if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false;
+1

onchange , AutoPostBack true, ASP.NET onchange script:

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

AutoPostBack false, onchange "confirm and __doPostBack" script (. , ) , , , __doPostBack.

+1
if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false;

. OnSelectedIndexChanged , OK CANCEL.

+1

, :

dropDown.SelectedIndexChanged += new EventHandler(dropDown_SelectedIndexChanged);

, . , .

dropDown.Attributes.Add("onchange", "javascript: return confirm('confirmation msg')");
0
&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;

inline "" , . .

0
source

Source: https://habr.com/ru/post/1696974/


All Articles