在下拉框的选定索引更改事件触发后的代码中实现 javascript 确认框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4747702/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing javascript confirm box in code behind on drop down box's selected index change event firing
提问by Serenity
Inside my boxLang_OnSelectedIndexChanged() event I got this :-
在我的 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 = "";
        }
Now what is happening is that. When I select a different item from the drop down list this confirm box does show up BUT it first clears the content of the text boxes and then this confirm box appears. What I want is that the textboxes content should be cleared ONLY when OK is clicked.
现在正在发生的事情就是这样。当我从下拉列表中选择不同的项目时,此确认框确实出现,但它首先清除文本框的内容,然后出现此确认框。我想要的是只有在单击确定时才应清除文本框内容。
What am I doing wrong? Please help.Thanks.
我究竟做错了什么?请帮忙。谢谢。
edit @jgauffin ** I typed return where you have. Now what is happening is that its just clearing the textboxes right away..didn't even show the confirm box this time! Whats wrong now?? Please reply..thnx**
编辑@jgauffin ** 我在你有的地方输入了 return 。现在发生的事情是它只是立即清除了文本框......这次甚至没有显示确认框!现在怎么了??请回复..thnx**
edit 2 I tried as follows:-
编辑 2 我试过如下:-
Inside my BindDropDown() method I added :-
在我的 BindDropDown() 方法中,我添加了:-
dropdownbox1.Attributes.Add("onChange","DisplayConfirmation()");
then in my aspx page I have added the following script as follows:-
然后在我的 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>
This function is not getting called when drop down's selected index is changed. What's wrong?
当下拉选择的索引更改时,不会调用此函数。怎么了?
edit 3:- ok I changed the line of code as follows:-
编辑 3:- 好的,我将代码行更改如下:-
 dropdownbox1.Attributes.Add("onchange", "javascript:return DisplayConfirmation()");
Still wont work. I am new to JavaScript and all so having problems. Help will be much appreciated.
还是不行。我是 JavaScript 的新手,所以遇到了问题。帮助将不胜感激。
回答by jgauffin
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "return confirm('Changing the language will clear the text in the textboxes. Click OK to proceed.');", true);
returnis needed to break the process on cancel.
需要return来中断取消过程。
回答by Andrew Johns
That's not going to work. You're posting back to the server, then registering the javascript confirm code, and then clearing content. The javascript confirm box will only actually appear once the page has reloaded, by which time you've already cleared the textbox values. It's like you're confusing clientside and server side programming. Client side code like javascript will only execute when the server has finished doing it's thing and the page is loaded.
那是行不通的。您正在回发到服务器,然后注册 javascript 确认代码,然后清除内容。javascript 确认框只会在页面重新加载后实际出现,此时您已经清除了文本框值。这就像您混淆了客户端和服务器端编程。像javascript这样的客户端代码只会在服务器完成它的事情并且页面被加载时才会执行。
What you need is this answer, probably:
你需要的是这个答案,可能是:

