Using AJAX to Dynamically Fill a Dropdown VB.Net JQuery -
i trying make ajax call fill dropdown when value selected in dropdown. currently, getting syntaxerror: invalid character error, if remove "datatype: json", mess looks html code. dropdown must bound using value , text pairs. aspx code related dropdown:
<%@ page title="" language="vb" autoeventwireup="false" masterpagefile="~/site.master" codebehind="request.aspx.vb" inherits="ndbedp.request" maintainscrollpositiononpostback="true" %> <asp:content id="content1" contentplaceholderid="headcontent" runat="server"> </asp:content> <asp:content id="content2" contentplaceholderid="featuredcontent" runat="server"> </asp:content> <asp:content id="content3" contentplaceholderid="maincontent" runat="server"> <script type="text/javascript"> $('.entitydrop').live('change', function (event) { loadstatedrop() }); var loadstatedrop = function () { var data = "{'entity':'"+ $('#<%=ddlentity.clientid%> option:selected').val()+"'}" alert("before ajax(), val: " + $('#<%=ddlentity.clientid%> option:selected').val()); $.ajax({ type: "post", url: "request.aspx/loadstates", datatype: "json", data: data, contenttype: "application/json; charset=utf-8", success: function (list) { alert(list); $('.statedrop').empty() $.each(list, function (element) { var optn = document.createelement("option"); optn.text = element.stateabbr; optn.value = element.stateid; $('.statedrop').append(optn); }); }, error: function (jqxhr, textstatus, errorthrown) { alert("error updating dropdowns: " + errorthrown); } }); }; </script> <asp:panel id="userinput" runat="server"> <asp:panel id="userinformation" runat="server" borderstyle="none" style="margin-bottom: 0px"> <asp:dropdownlist cssclass="entryfield, entitydrop" id="ddlentity" runat="server" width="95%"></asp:dropdownlist> <asp:dropdownlist cssclass="entryfield, statedrop" id="ddlexpensestate" runat="server" width="18%"></asp:dropdownlist> <asp:dropdownlist id="ddlexpenseperiod" runat="server" cssclass="entryfield" width="50%"></asp:dropdownlist> <asp:dropdownlist id="ddlexpenseyear" runat="server" cssclass="entryfield" width="20%"></asp:dropdownlist> </asp:panel> </asp:panel> </asp:content>
this vb.net code:
imports ndbedp.stringextensions imports ndbedp.dropdownextensions imports ndbedp.gridviewextensions imports system.io imports system.web.services imports system.web.script.services imports system.web.script.serialization public class request inherits system.web.ui.page <webmethod()> _ public shared function loadstates(entity integer) dictionary(of string, integer) dim expensestates dataset = (new requestbus).getexpensestates(entity) dim returnstates new dictionary(of string, integer) integer = 0 expensestates.tables(0).rows.count - 1 returnstates.add(expensestates.tables(0).rows(i).item("stateabbr"), expensestates.tables(0).rows(i).item("stateid")) next return returnstates end function end class
let me know if need see other code help.
i have tried doing through webservices, couldn't work either. included webservice , hooked properly, before adding of own code, got: javascript critical error @ line 3, column 1 web service script1002 syntax error. prefer in webservice, i'm open can work.
does know i'm doing wrong here?
edit: changed match current code
first off, not need serialize data coming asp.net ajax page method, json serializes data default.
second, why not returning type method? function
, there no return type declared. appears want return list(of t)
can loop through results client-side.
here example page method returns current time string
:
<webmethod> _ public shared function getdate() string return datetime.now.tostring() end function
please read become more familiar using jquery directly call asp.net ajax page methods
Comments
Post a Comment