0

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

If I do the same code in a normal HTML select it isn't a problem. What am I missing here?

3 Answers3

1

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?

Community
  • 1
  • 1
Razvan Dumitru
  • 10,549
  • 4
  • 31
  • 50
1

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 + "');");
Luizgrs
  • 4,595
  • 1
  • 19
  • 28
1

try like this

Remove this from markup

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

and add in page_load

 protected void Page_Load(object sender, EventArgs e)
  {

      ddlAddressSPC.Attributes.Add("onchange", "getCities('" + BusinessID + "')");
  }
Dgan
  • 9,618
  • 1
  • 28
  • 50