客户确认后的DropdownList自动回复

时间:2020-03-05 18:56:21  来源:igfitidea点击:

我有一个将autopostback设置为true的下拉列表。我想要
用户确认他们是否真的要更改该值,
在回发时会触发服务器端事件(selectedindexchanged)。

我尝试添加onchange属性" return Confirm('请单击OK进行更改。否则请单击CANCEL?';"
结果,并且如果取消,列表中的值不会还原
已选择。

当我从DropdownList标记中删除onchange属性时,页面会回发。添加onchange属性时不这样做。我仍然需要连接事件处理程序吗(我在C.Net 2.0上)。

任何线索都将有所帮助。

谢谢!

解决方案

回答

确保活动已连线:

dropDown.SelectedIndexChanged += new EventHandler(dropDown_SelectedIndexChanged);

我们还可以应用客户端属性以返回确认。如果取消,则相应地设置索引。

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

回答

我们可以通过调用我们在其中执行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"  />

回答

我们是否尝试过将onChange事件设置为javascript函数,然后在该函数内显示javascript警报并通过__doPostback函数(如果通过)?

IE。

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

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

回答

当前,我们总是返回confirm()的结果,因此,即使它返回true,我们仍将停止执行该事件,然后才能触发回发。onchange应该返回false;仅当confirm()也是如此时才是这样:

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

回答

如果将AutoPostBack设置为true,则覆盖onchange属性将不起作用,因为ASP.NET始终会将以下内容添加到onchange脚本的末尾:

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

如果将AutoPostBack设置为false,则可以使用" confirm and __doPostBack"类型的脚本覆盖onchange(请参见上文,下文err ..),但是我们可能必须手动创建__doPostBack函数。

回答

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

回答

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

始终返回,因此无论用户单击"确定"还是"取消",都会触发dropdownlist的OnSelectedIndexChanged事件。