[英]Implementing javascript confirm box in code behind on drop down box's selected index change event firing
在我的boxLang_OnSelectedIndexChanged()事件中我得到了这个: -
if (txtbox1.Text != "" && txtbox2.Text != "")
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "confirm('Changing the language will clear the text in the textboxes. Click OK to proceed.');", true);
txtbox1.Text = "";
txtbox2.Text = "";
}
现在发生的事情就是这样。 当我从下拉列表中选择一个不同的项目时,此确认框确实显示但是它首先清除文本框的内容,然后出现此确认框。 我想要的是,只有在单击“确定”时才应清除文本框内容。
我究竟做错了什么? 请帮忙。谢谢。
编辑@jgauffin **我输入你所在的地方。 现在发生的事情是它只是立即清除文本框..这次甚至都没有显示确认框! 什么错了? 请回复..thnx **
编辑2我尝试如下: -
在我的BindDropDown()方法中,我添加了: -
dropdownbox1.Attributes.Add("onChange","DisplayConfirmation()");
然后在我的aspx页面中,我添加了以下脚本: -
<script type="css/javascript">
function DisplayConfirmation() {
if (confirm('Changing the language will clear the text in the textboxes. Click OK to proceed.')) {
__doPostback('dropdownbox1','');
}
}
</script>
下拉列表的选定索引更改时,不会调用此函数。 怎么了?
编辑3: - 好吧我改变了代码行如下: -
dropdownbox1.Attributes.Add("onchange", "javascript:return DisplayConfirmation()");
仍然不会工作。 我是JavaScript的新手,所有这些都有问题。 将非常感谢帮助。
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "return confirm('Changing the language will clear the text in the textboxes. Click OK to proceed.');", true);
需要返回才能在取消时中断进程。
那不行。 您将回发到服务器,然后注册javascript确认代码,然后清除内容。 只有在页面重新加载后,javascript确认框才会实际显示,此时您已经清除了文本框值。 这就像你在混淆客户端和服务器端编程一样。 像javascript这样的客户端代码只会在服务器完成它并且加载页面时执行。
你需要的是这个答案,可能是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.