确保你的事件是有线的:
dropDown.SelectedIndexChanged += new EventHandler(dropDown_SelectedIndexChanged);
您还可以应用客户端属性来返回确认。如果取消,则相应地设置索引。
dropDown.Attributes.Add("onchange", "javascript: return confirm('confirmation msg')");
我有一个自ostback设置为true的下拉列表。我想让用户确认他们是否真的想更改值,这在回发时触发一个服务器端事件(selectedindexchanged)。
我已经尝试添加一个onchange属性'返回确认('请单击确定更改。否则单击CANCEL?';'),但无论确认结果如何,它都不会回发,如果选中了CANCEL,列表中的值也不会恢复。
当我从DropdownList标签中删除onchange属性时,页面会回发。当添加onchange属性时,则不需要。我是否仍然需要连接事件处理程序(我使用的是c# . net 2.0)。
任何线索都会有帮助。
谢谢!
确保你的事件是有线的:
dropDown.SelectedIndexChanged += new EventHandler(dropDown_SelectedIndexChanged);
您还可以应用客户端属性来返回确认。如果取消,则相应地设置索引。
dropDown.Attributes.Add("onchange", "javascript: return confirm('confirmation msg')");
你可以使用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" />
你有没有尝试设置onChange事件到一个javascript函数,然后在函数内部显示javascript警报,并利用__doPostback函数如果它通过?
即。
drpControl.Attributes("onChange") = "DisplayConfirmation();"
function DisplayConfirmation() {
if (confirm('Are you sure you want to do this?')) {
__doPostback('drpControl','');
}
}
当下拉列表触发部分回发时,以下工作:
// 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};");