【问题标题】:call javascript onSelectedIndexchanged in vb.net在 vb.net 中调用 javascript onSelectedIndexchanged
【发布时间】:2015-11-25 06:42:11
【问题描述】:

我想在下拉菜单 selectedindexchanged 上调用 javascript 函数。我试过这个

<asp:DropDownList ID="selectVehicle" AutoPostBack="true" OnSelectedIndexChanged="GetRoute(this.options[this.selectedIndex].value);" runat="server" CssClass="inners">
<asp:listitem Selected>-- Select Vehicle --</asp:listitem>
<asp:listitem Value="16">Tata Ace</asp:listitem>
<asp:listitem Value="28">Tata 407</asp:listitem>
</asp:DropDownList>

错误

BC30456:“GetRoute”不是“ASP.index3_aspx”的成员。

然后我尝试了 onChange 而不是 OnSelectedIndexChanged 但它对我没有用,因为在我的 js 函数中插入的文本框中的值正在工作但是当页面重新加载时文本框再次变为空白并且我无法关闭 AutoPostBack 因为它需要回发。有可能我可以运行 javascript OnSelectedIndexChanged 吗?

【问题讨论】:

  • 不,OnSelectedIndexChanged 是服务器端事件处理程序,您不能将客户端处理程序附加到它。你到底想做什么?
  • @RahulSingh on dropdownselection 我想调用 GetRoute();计算两个地方之间的距离并将其放在文本框中的脚本。如果我使用 OnChange 然后回发文本框再次变为空白。我只想在回发后将距离值保留在文本框中。
  • @RahulSingh 我可以将 GetRoute() 函数放在后面的代码中,然后 OnSelected IndexChanged 可能对我有用吗?如果可以的话,你能告诉我怎么做吗?
  • 是的,如果你无论如何都在做回发,那么在后面的代码中编写下拉更改事件处理程序中的逻辑。

标签: javascript asp.net vb.net


【解决方案1】:

你可以使用标准的javascript onchange:

<asp:DropDownList ID="selectVehicle" AutoPostBack="false" onchange="GetRoute(this.options[this.selectedIndex].value);" runat="server" CssClass="inners">
</asp:DropDownList>

假设 GetRoute 是一个 javascript 函数。

ASP.NET 将忽略 onchange 属性并将其呈现在结果选择元素中。

【讨论】:

  • 感谢您的回复,但我已经完成了,但我想要 autopostback="true" 我需要保持 AutoPostBack 启用。如果我保持启用,那么文本框会丢失它的值,该值由 GetRoute 函数计算。回发后是否有保留该值?
  • 如果你有回发 true 它将运行你定义的 onchange 然后 onselectedindexchange 服务器端函数。文本框是 asp.net 文本框吗?
  • 您使用的是传统的完整回发还是 Ajax 回发/脚本管理器?
【解决方案2】:

Rahul Singh 将 JS 代码写入服务器代码,并且只运行服务器代码,没有 Javascript,这很有意义。

如果由于某种原因你必须拥有 JS,你可以删除 autopostback = true,并手动调用 __doPostBack 参数,例如你的 JS 产生的值。

JavaScript: Multiple parameters in __doPostBack

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多