下拉列表自osback客户端确认后

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

我已经尝试添加一个onchange属性'返回确认('请单击确定更改。否则单击CANCEL?';'),但无论确认结果如何,它都不会回发,如果选中了CANCEL,列表中的值也不会恢复。

当我从DropdownList标签中删除onchange属性时,页面会回发。当添加onchange属性时,则不需要。我是否仍然需要连接事件处理程序(我使用的是c# . net 2.0)。

任何线索都会有帮助。

谢谢!

请先 登录 后评论

7 个回答

Brian Liang

确保你的事件是有线的:

dropDown.SelectedIndexChanged += new EventHandler(dropDown_SelectedIndexChanged);

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

dropDown.Attributes.Add("onchange", "javascript: return confirm('confirmation msg')");
请先 登录 后评论
Craig

你可以使用CustomValidator控件通过调用一个javascript函数来“验证”下拉菜单,在这个函数中你可以执行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"  />
请先 登录 后评论
Kyle Ballard

你有没有尝试设置onChange事件到一个javascript函数,然后在函数内部显示javascript警报,并利用__doPostback函数如果它通过?

即。

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

function DisplayConfirmation() {
  if (confirm('Are you sure you want to do this?')) {
    __doPostback('drpControl','');
  }
}
请先 登录 后评论
Billy Jo

当前,您总是返回的结果 confirm(),所以即使它返回 true,您仍然会在回发触发之前停止事件的执行。你的 3. 应该 return false; 只有当 confirm() 也像这样:

if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false;
请先 登录 后评论
Craig

如果您已经将AutoPostBack设置为true,则重写onchange属性将不起作用。NET将始终在onchange脚本的末尾追加以下内容:

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

如果你将AutoPostBack设置为false,那么用"confirm and __doPostBack"类型脚本重写onchange(参见上面,err..))将工作,但您可能必须手动创建__doPostBack函数。

请先 登录 后评论
JCallico

当下拉列表触发部分回发时,以下工作:

// 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};");
请先 登录 后评论
NealB
if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false;

总是返回,因此下拉列表的OnSelectedIndexChanged事件无论用户单击“确定”还是“取消”都会触发。

请先 登录 后评论
  • 26 关注
  • 0 收藏,274 浏览
  • Aamir 提出于 2022-11-02 18:55