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?

1
  • That puts the text <%=BusinessID %> inside the variable, I want to have the value of BusinessID in there, my function is in an external js file. Commented Oct 6, 2014 at 12:04

3 Answers 3

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 + "');");
Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

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 + "')");
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.