客户端确认后DropdownList autoposback

Aam*_*mir 3 asp.net onchange autopostback drop-down-menu

我有一个下拉列表,其中autopostback设置为true.我希望用户确认他们是否真的想要更改该值,这在post post上会触发服务器端事件(selectedindexchanged).

我尝试添加一个onchange属性"return confirm('请单击OK更改.否则单击CANCEL?';")但无论确认结果如何都不会回发,如果取消选中,列表中的值不会恢复.

当我从DropdownList标记中删除onchange属性时,页面会进行回发.添加onchange属性时不会.我还需要连接事件处理程序(我在C#.Net 2.0上).

任何线索都会有所帮助.

谢谢!

Kyl*_*ard 8

您是否尝试将onChange事件设置为javascript函数,然后在函数内部显示javascript警报并在其通过时使用__doPostback函数?

   
drpControl.Attributes("onChange") = "DisplayConfirmation();"

function DisplayConfirmation() {
  if (confirm('Are you sure you want to do this?')) {
    __doPostback('drpControl','');
  }
}
Run Code Online (Sandbox Code Playgroud)


Cra*_*aig 6

您可以通过调用执行confirm()的javascript函数来利用CustomValidator控件来"验证"下拉列表:

        <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"  />
Run Code Online (Sandbox Code Playgroud)


JCa*_*ico 6

当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};");
Run Code Online (Sandbox Code Playgroud)