我正在尝试在我的 Web 应用程序中填充自动完成功能。该列表是从数据表中填充的。出于某种原因,我无法让它显示在自动完成中。我知道我正在从查询中获得结果,因为我可以使用消息框来查看它们。在此示例中,我将结果限制为 5。
客户端代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AddToMonitoring.Default" %>
<head runat="server">
<script type="text/javascript">
$(document).ready(function () {
$("#<%=txtAutoComplete.ClientID%>").autocomplete('AutoCompleteHandler.ashx');
});
</head>
<body>
<div id="inputDIV" style="font-size: x-large; font-family: Calibri;
position: relative; top: 25px; left: 25px; height: 638px; width: 1239px;">
Asset Name: <asp:TextBox id="txtAutoComplete" runat="server" />
</div>
</body>
这是处理程序页面:
public class AutoCompleteHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
dsCIInfoTableAdapters.BMC_CORE_BMC_ComputerSystemTableAdapter serverTableAdapter1;
serverTableAdapter1 = new dsCIInfoTableAdapters.BMC_CORE_BMC_ComputerSystemTableAdapter();
dsCIInfo.BMC_CORE_BMC_ComputerSystemDataTable newServerTable;
newServerTable = serverTableAdapter1.GetData();
var query = (from row in newServerTable.AsEnumerable()
where row.Field<string>("Item") == "Server"
select row.Field<string>("Name")).Distinct().Take(5);
var queryArray = query.ToArray();
StringBuilder sb = new StringBuilder();
foreach (string row in queryArray)
{
context.Response.Write(sb.Append(row).Append(Environment.NewLine));
}
}
public bool IsReusable
{
get
{
return false;
}
}
}