ASP.NET/VB.NET:Dropdownlist SelectedIndexChanged不触发onchange =“javascript:return true;” [英] ASP.NET/VB.NET: Dropdownlist SelectedIndexChanged not firing with onchange="javascript:return true;"

查看:346
本文介绍了ASP.NET/VB.NET:Dropdownlist SelectedIndexChanged不触发onchange =“javascript:return true;”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下标记:

 < asp:DropDownList ID =dd1AutoPostBack =truerunat = 服务器 > 
< asp:ListItem Value =1> 1< / asp:ListItem>
< asp:ListItem Value =2> 2< / asp:ListItem>
< / asp:DropDownList>
< asp:DropDownList ID =dd2AutoPostBack =trueonchange =javascript:return true; RUNAT = 服务器 >
< asp:ListItem Value =1> 3< / asp:ListItem>
< asp:ListItem Value =2> 4< / asp:ListItem>
< / asp:DropDownList>有线直到:



$ $ $ $ $ $ $ $ $ $ $ $ $ p> 受保护的Sub changed1(发件人作为对象,e作为EventArgs)处理dd1.SelectedIndexChanged

End Sub

受保护的Sub changed2(sender As Object, e As EventArgs)处理dd2.SelectedIndexChanged

End Sub

当dd2的索引改变了,你会期望它的处理程序开火,对吧?那么它没有。相反,它会被排队,并在dd1的处理程序在其索引更改后触发。如果你把 onchange =javascript:return true;关闭dd2,它会很好的。



任何人都知道这里发生了什么?



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



更新:我可以强制服务器事件通过这样做在Javascript :



__ doPostBack(<%= dd2.ClientID%>,''); p>

我不明白为什么我必须这样做,但它的作品。但是,我还是要这么做,所以如果有人知道,请让我知道,所以我可以把你标记为答案。

解决方案

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



我最后所做的是检查函数中的条件。如果为true,则会触发此操作:



__ doPostBack(<%= dd2.ClientID%>,');



否则,它不会。


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.

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:

__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.

解决方案

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;").

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

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

Otherwise, it doesn't.

这篇关于ASP.NET/VB.NET:Dropdownlist SelectedIndexChanged不触发onchange =“javascript:return true;”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆