ASP.NET/VB.NET:Dropdownlist SelectedIndexChanged 不触发 onchange="javascript:return true;"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7918628/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:03:44  来源:igfitidea点击:

ASP.NET/VB.NET: Dropdownlist SelectedIndexChanged not firing with onchange="javascript:return true;"

javascriptasp.neteventsdrop-down-menuonchange

提问by oscilatingcretin

I have the following markup:

我有以下标记:

<asp:DropDownList ID="dd1" AutoPostBack="true" runat="server">
    <asp:ListItem Value="1">1</asp:ListItem>
    <asp:ListItem Value="2">2</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="dd2" AutoPostBack="true" onchange="javascript:return true;" runat="server">
    <asp:ListItem Value="1">3</asp:ListItem>
    <asp:ListItem Value="2">4</asp:ListItem>
</asp:DropDownList>

Wired up to this:

连线到这个:

Protected Sub changed1(sender As Object, e As EventArgs) Handles dd1.SelectedIndexChanged

End Sub

Protected Sub changed2(sender As Object, e As EventArgs) Handles dd2.SelectedIndexChanged

End Sub

When dd2's index is changed, you'd expect its handler to fire, right? Well, it doesn't. Instead, it gets "queued up" and is fired after dd1's handler fires when its index is changed. If you take the onchange="javascript:return true;"off dd2, it fires just fine.

当 dd2 的索引更改时,您会期望它的处理程序触发,对吗?好吧,它没有。相反,它会“排队”并在 dd1 的处理程序在其索引更改时触发后被触发。如果你onchange="javascript:return true;"关闭 dd2,它就可以正常工作。

Does anyone have any idea what's happening here?

有谁知道这里发生了什么?

Edit: My first answer would be that using return expressions on a dropdownlist doesn't work the same as a button's click event, but I swear I've done this with dropdownlists before.

编辑:我的第一个答案是在下拉列表上使用返回表达式与按钮的点击事件不同,但我发誓我以前用下拉列表做过这个。

Update:I am able to force the server event to fire by doing this in Javascript:

更新:我可以通过在 Javascript 中执行此操作来强制服务器事件触发:

__doPostBack("<%=dd2.ClientID %>", '');

__doPostBack("<%=dd2.ClientID %>", '');

I don't see why I have to do this, but it works. However, I still want to do it the other way, so if someone knows, please let me know so I can mark you as answer.

我不明白为什么我必须这样做,但它有效。但是,我仍然想以另一种方式来做,所以如果有人知道,请告诉我,以便我将您标记为答案。

采纳答案by oscilatingcretin

For some reason, I thought you could cancel a dropdown's server event by returning false on the client side's onchange event like you could with a button's onclick event (eg, onclick="javascript:return false;").

出于某种原因,我认为您可以通过在客户端的 onchange 事件上返回 false 来取消下拉列表的服务器事件,就像使用按钮的 onclick 事件(例如,onclick="javascript:return false;")一样。

What I ended up doing is checking a condition in a function. If true, it fires this:

我最终做的是检查函数中的条件。如果为真,它会触发:

__doPostBack("<%=dd2.ClientID %>", '');

__doPostBack("<%=dd2.ClientID %>", '');

Otherwise, it doesn't.

否则,它不会。

回答by James Johnson

You shouldn't need that at all. Just set AutoPostBackto true, and if you need to escape validation set CausesValidationto false.

你根本不需要那个。只需设置AutoPostBack为 true,如果您需要将验证设置CausesValidation为 false。

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" CausesValidation="false" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" />

回答by Kelly

<asp:DropDownList ID="page_size" runat="server" **AutoPostBack="true"** OnSelectedIndexChanged="page_size_SelectedIndexChanged">
                            </asp:DropDownList>

Add Autopostback="truedid the trick for me.

添加Autopostback="true对我有用。

回答by Shyam Sharma

__doPostBack("<%=dd2.ClientID %>", '');

This worked for me.

这对我有用。

This was my drop down:

这是我的下拉菜单:

<asp:DropDownList ID="ddlbranchname" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlBranchChanged" 
onchange="return CheckDate();" CausesValidation="false" CssClass="dropdown">
</asp:DropDownList>

Here is my jquery function:

这是我的 jquery 函数:

function CheckDate() {
    var date = document.getElementById('<%= ucDateTimeStart.FindControl("txtDateTime").ClientID %>').value;
    if (date == '') {
        alert("Please select a valid date.");
        return false;
    }
    else {
        __doPostBack("<%=ddlbranchname.ClientID %>", '');
        return true;
    }
    return true;
}