Skip to main content
 首页 » 编程设计

c#之在下拉框的选定索引更改事件触发后的代码中实现 javascript 确认框

2024年02月23日40落叶

在我的 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 ** 我在你输入的地方输入了 return。现在发生的事情是它只是立即清除文本框..这次甚至没有显示确认框!现在怎么了??请回复..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); 

return 需要中断取消过程。