I created a dropdown list dynamically. I need to add an onchange
event dynamically and do a postback so I can make a database call. When I change the dropdown list item, I get the error:
ReferenceError: UpdateLocationList is not defined.
This is my javascript code:
<head runat="server">
<title>Locations</title>
<meta http-equiv="Content-Language" content="en-us"/>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<script src = "scripts/jquery-1.11.2.min.js" type = "text/javascript"></script>
<script type="text/javascript">
function UpdateLocationList(obj) {
alert('In function');
var facilityValue = document.getElementById("FacilityTypeDDL").value;
alert(facilityValue);
__doPostBack('FacilityTypeDDL', facilityValue);
}
</script>
</head>
<body>
<%ListLocations()%>
</body>
This is the ListLocations() method in the code behind:
Public Sub ListLocations()
Response.Write("<div style='text-align:left;'>")
Response.Write("<label>Facility Type:<select id='FacilityTypeDDL' name='FacilityTypeDDL' size='1' runat='server' onchange='UpdateLocationList(this)'>")
For Each facility As ListItem In lstBoxFacilityTypes
'Get the first value in the list and use the ID to to the list of locations below
If String.Compare(facilityValueSelected, "") = 0 Then
facilityValueSelected = facility.Value
End If
Response.Write("<option value=''" & facility.Value & "''>" & facility.Text & "</option>")
Next
Response.Write("</select></label>")
Response.Write("</div>")
Response.Write("<hr>")
End Sub
What is missing that would cause the error that the function, UpdateLocationList, is not defined?
UPDATE I added the dropdownlist directly to the aspx page as an asp:DropDownList:
<body>
<form id="form1" runat="server">
<div style='text-align:center;'>
<a style='text-decoration:none;font-size:16px;color:blue;background-color:white;width:200px;padding:4px;' href='LocationDetails.aspx?Location_ID=0' target='detailPanel'> Add Location
</a></div>
  
<asp:Label ID="FacilityTypeLbl" runat="server">Facility Type:</asp:Label>
<asp:DropDownList ID="FacilityTypeDDL" runat="server" AutoPostBack="true" OnSelectedIndexChanged=" FacilityTypeDDL_SelectedIndexChanged">
</asp:DropDownList>
<hr/>
<%ListLocations()%>
</form>
</body>
Add the OnSelectedChanged event to the code behind:
Protected Sub FacilityTypeDDL_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles FacilityTypeDDL.SelectedIndexChanged
strFacilityValue = FacilityTypeDDL.SelectedValue
ListLocations()
End Sub
I put a breakpoint in this method and it does not stop within it. Why is my event not firing?