[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class CasCadingDropDown : System.Web.Services.WebService { //private DataSet ds = new DataSet(); //private DataTable dt = new DataTable(); public CasCadingDropDown() { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public CascadingDropDownNameValue[] GetStates(string knownCategoryValues) { string query = "SELECT statename, stateid FROM tblstates"; List<CascadingDropDownNameValue> states = GetData(query); return states.ToArray(); } [WebMethod] public CascadingDropDownNameValue[] GetDistricts(int state) { //int state = Formats.ConvertToInt(CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["stateid"]); string query = string.Format("select districtname,districtid from tbldistricts where stateid={0}",state); List<CascadingDropDownNameValue> districts = GetData(query); return districts.ToArray(); } private List<CascadingDropDownNameValue> GetData(string query) { string connstring = String.Format("Server=192.168.1.100;Port=5432;Encoding=UNICODE;User Id=postgres;Password=sggkishore;Database=SNCF;"); NpgsqlCommand cmd = new NpgsqlCommand(query); List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); using (NpgsqlConnection conn = new NpgsqlConnection(connstring)) { conn.Open(); cmd.Connection = conn; using (NpgsqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { values.Add(new CascadingDropDownNameValue { name = reader[0].ToString(), value = reader[1].ToString() }); } reader.Close(); conn.Close(); return values; } } }
<asp:DropDownList ID="ddlCState" runat="server" CssClass ="userentry" > <cc1:CascadingDropDown ID="cdlStates" TargetControlID="ddlCState" PromptText="Select State" ServicePath="CascadingDropdown.asmx" ServiceMethod="GetStates" runat="server" Category="StateId" />
[ScriptMethod]
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)