本文介绍了用于地址查找的Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好 我正在为地址构建一个Web API。 如果用户输入邮政编码,其余的文本框应该填充正确的值。 我已经做了一些事情,但完整的地址显示在一个文本框中。 可以请一些人帮助我 。Hi AllI am building a web API for Address.If a user enters postcode , the rest of the text boxes should be populated with the correct values.I have done something but the complete address is showing in one single text box.Can please some one help me.<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AutoCompletetextBox.aspx.cs" Inherits="AutoTextBox.AutoCompletetextBox" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>AutoComplete Textbox with webservice using jQuery</title><link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script><script type="text/javascript"> $(document).ready(function () { SearchText(); }); function SearchText() { $(".autosuggest").autocomplete({ source: function (request, response) { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "AutoCompleteService.asmx/GetAutoCompleteData", data: "{'postcode':'" + document.getElementById('txtSearch').value + "'}", dataType: "json", success: function (data) { response(data.d); }, error: function (result) { alert("Please Enter Valid PostCode"); } }); } }); }</script></head><body><form id="form1" runat="server"><div class="demo"><div class="ui-widget"><label for="tbAuto">Post Code: </label><input type="text" id="txtSearch" class="autosuggest" /><br /> <label for="tbAuto">Building Number: </label><input type="text" id="txtSearch1" class="autosuggest" /><br /> <label for="tbAuto">Building Name: </label><input type="text" id="txtSearch2" class="autosuggest" /></div></div></form></body></html> 。.namespace AutoTextBox{ /// <summary> /// Summary description for AutoCompleteService /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 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 AutoCompleteService : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public List<string> GetAutoCompleteData(string postcode) { List<string> result = new List<string>(); using (SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True")) { using (SqlCommand cmd = new SqlCommand("select (CAST(ISNULL(BuildingNumber,0) AS NVARCHAR(20)) +' '+ ISNULL(BuildingName,'') +' '+ ISNULL(Postcode,'')) as CompleteAddress from AddressRecord where( CAST(ISNULL(BuildingNumber,0) AS NVARCHAR(20)) +' '+ ISNULL(BuildingName,'') +' '+ ISNULL(Postcode,'') )like '%'+@SearchText+'%'", con)) { con.Open(); cmd.Parameters.AddWithValue("@SearchText", postcode); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { result.Add(dr["CompleteAddress"].ToString()); } return result; } } } }} 请所有人可以指导我。 谢谢Please anyone can guide me.Thanks推荐答案 。.namespace AutoTextBox{ /// <summary> /// Summary description for AutoCompleteService /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 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 AutoCompleteService : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public List<string> GetAutoCompleteData(string postcode) { List<string> result = new List<string>(); using (SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True")) { using (SqlCommand cmd = new SqlCommand("select (CAST(ISNULL(BuildingNumber,0) AS NVARCHAR(20)) +' '+ ISNULL(BuildingName,'') +' '+ ISNULL(Postcode,'')) as CompleteAddress from AddressRecord where( CAST(ISNULL(BuildingNumber,0) AS NVARCHAR(20)) +' '+ ISNULL(BuildingName,'') +' '+ ISNULL(Postcode,'') )like '%'+@SearchText+'%'", con)) { con.Open(); cmd.Parameters.AddWithValue("@SearchText", postcode); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { result.Add(dr["CompleteAddress"].ToString()); } return result; } } } }} 请所有人可以指导我。 谢谢Please anyone can guide me.Thanks 这篇关于用于地址查找的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 17:46