DropdownList autoposback after client confirmation

不打扰是莪最后的温柔 提交于 2019-11-30 18:24:59

问题


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!


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



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



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



回答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;



回答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.




回答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.




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



回答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.



来源:https://stackoverflow.com/questions/73748/dropdownlist-autoposback-after-client-confirmation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!