ASP.NET / C# - Ajax Cascading DropDown - Second DDL Not Populating -
i'm creating simple web form driven cascading ddl. first list populates list of given values; second populated sql query selected value in first ddl acting parameter second's query. seems simple enough piecing examples found online, but, unfortunately, second ddl not populating @ all. i've checked query fine , populate when clause / parameter eliminated.
here's code:
c#
using ajaxcontroltoolkit; using system; using system.collections.generic; using system.linq; using system.web; using system.web.services; using system.data; using system.data.sqlclient; using system.configuration; namespace web_form___chart_review { /// <summary> /// summary description programcs /// </summary> [webservice(namespace = "http://tempuri.org")] [webservicebinding(conformsto = wsiprofiles.basicprofile1_1)] [system.componentmodel.toolboxitem(false)] // allow web service called script, using asp.net ajax, uncomment following line. [system.web.script.services.scriptservice] public class programcs : system.web.services.webservice { list<division> division = new list<division>(); [webmethod] public cascadingdropdownnamevalue[] getdivision(string knowncategoryvalues, string category) { division.add(new division { divid = "mh", name = "mental health" }); division.add(new division { divid = "sud", name = "substance use disorder" }); division.add(new division { divid = "gam", name = "gambling" }); division.add(new division { divid = "%", name = "lmp" }); list<cascadingdropdownnamevalue> l = new list<cascadingdropdownnamevalue>(); var x = c in division select c; foreach (division divisions in x) { l.add(new cascadingdropdownnamevalue(divisions.name,divisions.divid)); } return l.toarray(); } [webmethod] public cascadingdropdownnamevalue[] getsite(string knowncategoryvalues, string category) { string sel_division = cascadingdropdown.parseknowncategoryvaluesstring(knowncategoryvalues)["divid"]; string query = string.format("select distinct [sitename],[site] chartreview.dbo.vw_programlist [category] '{0}'",sel_division); list<cascadingdropdownnamevalue> sites = getdata(query); return sites.toarray(); } public class division { public string divid {get;set;} public string name {get;set;} } private list<cascadingdropdownnamevalue> getdata(string query) { string constring = configurationmanager.connectionstrings["sqlserver"].connectionstring; sqlcommand cmd = new sqlcommand(query); list<cascadingdropdownnamevalue> values = new list<cascadingdropdownnamevalue>(); using (sqlconnection con = new sqlconnection(constring)) { con.open(); cmd.connection = con; using (sqldatareader reader = cmd.executereader()) { while (reader.read()) { values.add(new cascadingdropdownnamevalue { name = reader[0].tostring(), value = reader[1].tostring() }); } reader.close(); con.close(); return values; } } } } }
and html:
<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="web_form___chart_review.default" enableeventvalidation="false" %> <%@ register assembly="ajaxcontroltoolkit" namespace="ajaxcontroltoolkit" tagprefix="ajaxtoolkit" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:scriptmanager id="asm" runat="server"></asp:scriptmanager> <div> division creating tally?<br /> <asp:dropdownlist id="ddlselectdivision" runat="server"></asp:dropdownlist> <ajaxtoolkit:cascadingdropdown id="ddlselectdivision_cascadingdropdown" runat="server" targetcontrolid="ddlselectdivision" servicepath="programcs.asmx" servicemethod="getdivision" category="division" prompttext="select division" selectedvalue="mh" promptvalue="mh" loadingtext="loading..." /> <br /> <asp:dropdownlist id="ddlselectsite" runat="server"></asp:dropdownlist> <ajaxtoolkit:cascadingdropdown id="ddlselectsite_cascadingdropdown" runat="server" targetcontrolid="ddlselectsite" servicepath="programcs.asmx" servicemethod="getsite" category="site" prompttext="select site" promptvalue="" loadingtext="loading..." /> </div> </form> </body> </html>
it might simple making sure first dropdown has autopostback="true" ensure second 1 picks selected value.
Comments
Post a Comment