, ; jQuery HttpHandler , , ; . , , .
-, HttpHandler. , , , . , , ( , ), JSON .
using Newtonsoft.Json;
namespace Invoicing.HttpHandlers
{
[WebService(Namespace = "yournamespace/http-handlers/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class CustomerHandler : IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable
{
get
{
return false;
}
}
public void ProcessRequest(HttpContext context)
{
}
}
, JSON.
, -, ResponseCustomer, , : -
[Serializable]
public class ResponseCustomer
{
public int ID;
public string CustomerName;
}
IHttpHandler.ProcessRequest IList - , JSON : -
public void ProcessRequest(HttpContext context)
{
string json = string.Empty;
if (!string.IsNullOrEmpty(context.Request["term"]))
{
string searchTerm = context.Request["term"];
var customers = (data access component).CustomerSearch(searchTerm);
if (customers.Count != 0)
{
var transformList = new List<ResponseCustomer>();
for (int index = 0; index < customers.Count; index++)
{
transformList.Add(new ResponseCustomer
{
ID = customers[index].ID,
CustomerName = customers[index].CustomerName
});
}
json = JsonConvert.SerializeObject(transformList);
}
}
context.Response.Write(json);
}
?
, HttpHandler web.config( , - IIS6, IIS 7+): -
<system.web>
<httpHandlers>
<add path="customerHandler.ashx" verb="*" type="(namespace).(handler name), (assembly name)" />
i.e.
<add path="customerHandler.ashx" verb="*" type="MyProject.Handlers.CustomerHandler, MyProject" />
and for IIS7: -
<system.webServer>
<handlers>
<add name="customerHandler" preCondition="integratedMode" verb="*" path="customerHandler.ashx" type="(namespace).(handler name), (assembly name)"" />
, , : -
HTML: -
<span>Customer</span>
<span class="ui-widget" style="display:inline-block">
<input id="txtCustomer" runat="server" clientidmode="Static" />
</span>
JS: -
$(function ()
{
$("#txtCustomer").autocomplete(
{
source: "customerHandler.ashx",
minLength: 3,
select: function (event, ui)
{
if (ui.item)
{
$("#txtCustomer").val(ui.item.CustomerName);
return false;
}
}
})
.data("autocomplete")._renderItem = function (ul, item)
{
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a><table cellpadding='0' cellspacing='0' border='0' width='250px'><tr><td width='200' valign='top' align='left'>"
+ item.CustomerName + "</td><td width='50px' valign='top' align='left'>[ID "
+ item.ID + "]</td></tr></table></a>")
.appendTo(ul);
};
});
, , , .