简体   繁体   English

将变量传递给javascript函数在asp:dropdownlist中失败

[英]passing variable to javascript function fails in asp:dropdownlist

Currently I have a function called in the following way: 目前,我有一个以以下方式调用的函数:

<script type="text/javascript">
getCities('<%=BusinessID %>');
</script>

This works without any problem, now I want to do the same on an onchange event in a asp:dropdownlist as following: 这可以正常工作,现在我想对asp:dropdownlist中的onchange事件执行相同的操作,如下所示:

<asp:DropDownList ID="ddlAddressSPC" runat="server" clientidmode="Static" AutoPostBack="False" onchange="javascript:getCities('<%=BusinessID %>');" Width="306px" CssClass="txt12NormalLeft" ToolTip="Select State|Province|County" />

But now the ASP.net variable isn't evaluated and is passed as <%=BusinessID %> instead of the value. 但是现在不评估ASP.net变量,而是将其作为<%= BusinessID%>而不是值传递。

If I do the same code in a normal HTML select it isn't a problem. 如果我在普通HTML中执行相同的代码,则没有问题。 What am I missing here? 我在这里想念什么?

This should work. 这应该工作。

<script type="text/javascript">

var currentBusinessIdString = '<%=BusinessID %>';

var currentBusinessId = parseInt(currentBusinessIdString);

getCities(currentBusinessId);

</script>

If this didn't help you, you may try some issues listed here : How do I give JavaScript variables data from ASP.NET variables? 如果这没有帮助您,您可以尝试尝试以下此处列出的问题: 如何从ASP.NET变量中获取JavaScript变量数据?

You can't do that directly at the server element tag. 您不能直接在服务器元素标签上执行此操作。

You should do that somewhere in the code behind: 您应该在后面的代码中的某处执行此操作:

ddlAddressSPC.Attributes.Add("onchange", "javascript:getCities('" + BusinessID + "');");

try like this 这样尝试

Remove this from markup 从标记中删除

onchange="javascript:getCities('<%=BusinessID %>');"

and add in page_load 并添加page_load

 protected void Page_Load(object sender, EventArgs e)
  {

      ddlAddressSPC.Attributes.Add("onchange", "getCities('" + BusinessID + "')");
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM