Answered by:
Would like to create a web service in C# from JavaScript

Question
-
User-1164658185 posted
http://vikku.info/programming/geodata/geonames-get-country-state-city-hierarchy.htm
Is it possible to put this in C#? i started to write the web service.
<script type="text/javascript"> var whos=null; function getplaces(gid,src) { whos = src // var request = "http://ws.geonames.org/childrenJSON?geonameId="+gid+"&callback=getLocation&style=long"; var request = "http://www.geonames.org/childrenJSON?geonameId="+gid+"&callback=listPlaces&style=long"; aObj = new JSONscriptRequest(request); aObj.buildScriptTag(); aObj.addScriptTag(); } function listPlaces(jData) { counts = jData.geonames.length<jData.totalResultsCount ? jData.geonames.length : jData.totalResultsCount who = document.getElementById(whos) who.options.length = 0; if(counts)who.options[who.options.length] = new Option('Select','') else who.options[who.options.length] = new Option('No Data Available','NULL') for(var i=0;i<counts;i++) who.options[who.options.length] = new Option(jData.geonames[i].name,jData.geonames[i].geonameId) delete jData; jData = null } window.onload = function() { getplaces(6295630,'continent'); } </script> </head> <div class="contents" style="width:55%; margin:0px auto;"> <p><span>Continent:</span> <select name="continent" id="continent" onchange="getplaces(this.value,'country');"> <option value=""></option> </select> </p> <p><span>Country:</span> <select name="country" id="country" onchange="getplaces(this.value,'province');"> <option value=""></option> </select> </p> <p><span>State / Provice:</span> <select name="province" id="province" onchange="getplaces(this.value,'region')"> <option value=""></option> </select> </p> <p><span>County / Region:</span> <select name="region" id="region" onchange="getplaces(this.value,'city')"> <option value=""></option> </select> </p> <p><span>City:</span> <select name="city" id="city"> <option value=""></option></select> </p> </div>
[WebMethod] public CascadingDropDownNameValue[] GetPlaces(string knownCategoryValues, string category) { var gids = new List<GID>(); int gidId; StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); if (!kv.ContainsKey("gid") || !Int32.TryParse(kv["gid"], out gidId)) { return null; } XDocument request = XDocument.Load(@"http://www.geonames.org/childrenJSON?geonameId=" + Convert.ToString(gidId) + "&callback=listPlaces&style=long"); //aObj = new JSONscriptRequest(request); //aObj.buildScriptTag(); //aObj.addScriptTag(); List<CascadingDropDownNameValue> gidNames = new List<CascadingDropDownNameValue>(); //try //{ // foreach (var gid in gidList) // { // string strGIDID = gid.; // string strGIDName = gid.Name; // gidNames.Add(new CascadingDropDownNameValue(strRegName, strRegID)); // } //} return gidNames.ToArray(); }
Saturday, May 4, 2013 1:49 AM
Answers
-
User1116504247 posted
Use javascript pagemethods, so that you can write the code in your c# file only...try this sample
<script type='text/javascript'> function GetMessage() { var val = document.getElementById('id').value; getcitystate(val); } function getcitystate(val) { try { PageMethods.ZIP(val, OnGetMessageSuccess, OnGetMessageFailure); } catch (ex) { alert(ex); } } function OnGetMessageSuccess(result, userContext, methodName) { // success function. Here you will get the value from your method } function OnGetMessageFailure(error, userContext, methodName) { alert(error.get_message()); } </script> add below code in ur c# file... [System.Web.Services.WebMethod] public static string[] ZIP(string val) { DataTable dt = new DataTable(); string city = ""; string state = ""; string county = ""; List<string> lst = new List<string>(); if (dt.Rows.Count > 0) { city = dt.Rows[0]["CITY"].ToString(); lst.Add(city); state = dt.Rows[0]["STATE"].ToString(); lst.Add(state); county = dt.Rows[0]["COUNTY"].ToString(); lst.Add(county); } else { lst.Add(city); lst.Add(state); lst.Add(county); } return lst.ToArray(); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 4, 2013 8:09 AM