防止在 js 中为 Web 表单下拉列表回发
prevent postback in js for a webform dropdown
我正在开发一个 asp.net 网络表单应用程序。在一个页面中,我有一个包含一些值的下拉列表("a"、"b"、"c"、...)。当我 select 此下拉列表中的值时,会引发服务器端事件,我将 selected 值写入我的数据库中。
这是代码:
<asp:DropDownList ID="myDdl" runat="server" OnSelectedIndexChanged="server_handler" AutoPostBack="True"/>
protected void server_handler(object sender, EventArgs e)
{
myUpdateMethod(this.myDdl.selectedValue);
}
这工作得很好,但现在我想在 selected 值时在我的客户端询问确认,我们真的想更新我的数据库中的值。
如果我们在我的 js 确认对话框中 select 是,我们会像以前一样继续并调用服务器,否则我们会停止回发。但这是我做不到的,我无法停止回发,这是我试过的:
<asp:DropDownList ID="myDdl" runat="server" OnSelectedIndexChanged="server_handler" AutoPostBack="True" onchange="confirmornot(event)"/>
function confirmornot(event)
{
var str = confirm("do you want to continue?")
if (!str)// the user select no and this is where I am trying to stop to calling the server handler function. Basically there, I want here nothing to happen
{
//solution 1
event.preventDefault();
//solution 2
event.stopPropagation() or event.stopImmediatePropagation()
//solution 3
return false or return true
}
None 这些解决方案有效,无论我输入什么,服务器端函数都会被调用,我认为这是因为我的 autpostback="true" 在我的下拉列表中,但是如果我删除
这样,那我就陷入了相反的问题,我的服务器端函数永远不会被调用。
在此先感谢您的帮助
你可以试试这个:
将 ClientIDMode="Static" 设置为 asp 网络下拉列表,这样您将拥有静态 ID "myDdl" 并将自动回发设置为 false
在 confirmornot 方法而不是 return 语句中,尝试
__doPostBack('myDdl');
我有一个 hacky 解决方案给你
HTMl
添加隐藏字段
<asp:HiddenField runat="server" ID="hdntocheck" />
DDL 标记
<asp:DropDownList ID="myDdl" runat="server" OnSelectedIndexChanged="server_handler" AutoPostBack="True" onchange="confirmornot()"/>
Js
function confirmornot() {
if (!confirm("do you want to continue?"))
{
hdntocheck.value = "false";
}
else {
hdntocheck.value = "true";
}
}
在Cs
protected void server_handler(object sender, EventArgs e)
{
if( hdntocheck.value =="true")
{
myUpdateMethod(this.myDdl.selectedValue);
}
}
我正在开发一个 asp.net 网络表单应用程序。在一个页面中,我有一个包含一些值的下拉列表("a"、"b"、"c"、...)。当我 select 此下拉列表中的值时,会引发服务器端事件,我将 selected 值写入我的数据库中。 这是代码:
<asp:DropDownList ID="myDdl" runat="server" OnSelectedIndexChanged="server_handler" AutoPostBack="True"/>
protected void server_handler(object sender, EventArgs e)
{
myUpdateMethod(this.myDdl.selectedValue);
}
这工作得很好,但现在我想在 selected 值时在我的客户端询问确认,我们真的想更新我的数据库中的值。 如果我们在我的 js 确认对话框中 select 是,我们会像以前一样继续并调用服务器,否则我们会停止回发。但这是我做不到的,我无法停止回发,这是我试过的:
<asp:DropDownList ID="myDdl" runat="server" OnSelectedIndexChanged="server_handler" AutoPostBack="True" onchange="confirmornot(event)"/>
function confirmornot(event)
{
var str = confirm("do you want to continue?")
if (!str)// the user select no and this is where I am trying to stop to calling the server handler function. Basically there, I want here nothing to happen
{
//solution 1
event.preventDefault();
//solution 2
event.stopPropagation() or event.stopImmediatePropagation()
//solution 3
return false or return true
}
None 这些解决方案有效,无论我输入什么,服务器端函数都会被调用,我认为这是因为我的 autpostback="true" 在我的下拉列表中,但是如果我删除 这样,那我就陷入了相反的问题,我的服务器端函数永远不会被调用。
在此先感谢您的帮助
你可以试试这个:
将 ClientIDMode="Static" 设置为 asp 网络下拉列表,这样您将拥有静态 ID "myDdl" 并将自动回发设置为 false
在 confirmornot 方法而不是 return 语句中,尝试 __doPostBack('myDdl');
我有一个 hacky 解决方案给你
HTMl
添加隐藏字段
<asp:HiddenField runat="server" ID="hdntocheck" />
DDL 标记
<asp:DropDownList ID="myDdl" runat="server" OnSelectedIndexChanged="server_handler" AutoPostBack="True" onchange="confirmornot()"/>
Js
function confirmornot() {
if (!confirm("do you want to continue?"))
{
hdntocheck.value = "false";
}
else {
hdntocheck.value = "true";
}
}
在Cs
protected void server_handler(object sender, EventArgs e)
{
if( hdntocheck.value =="true")
{
myUpdateMethod(this.myDdl.selectedValue);
}
}