我有一个通用处理程序,所以我可以使用 jquery.ui 自动完成,但处理程序没有触发。
这是我的代码:ASPX
<%--------- -------- Autocomplete ----------- --%>
<link type="text/css" href="../css/ui-lightness/jquery.ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="../js/jquery.ui.widget.js"></script>
<script type="text/javascript" src="../js/jquery.ui.position.js"></script>
<script type="text/javascript" src="../js/jquery.ui.autocomplete.js"></script>
<link href="../css/demos.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function jqueryUI_autocomplete_update_backgroundColor(textbox) {
var loginId = $(textbox).next().val();
if (!textbox.disabled && loginId == "")
textbox.style.backgroundColor = "#ff9999";
else
textbox.style.backgroundColor = "";
}
$(function () {
$("#user").autocomplete({
source: "Handler1.ashx",
minLength: 1,
select: function (event, ui) {
$(this).next().val(ui.item.id);
$("input[id=UserId]")[0].value = ui.item.id;
$("input[id=DisplayName]")[0].value = ui.item.value;
jqueryUI_autocomplete_update_backgroundColor(this);
},
search: function (event, ui) {
$(this).next().val('');
$("input[id=UserId]")[0].value = '';
$("input[id=DisplayName]")[0].value = '';
jqueryUI_autocomplete_update_backgroundColor(this);
}
})
$("#user").data("autocomplete")._renderItem = function (ul, item)
{
item.label = item.label.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(this.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + item.label + "</a>")
.appendTo(ul);
};
});
</script>
Handler1.ashx
/// <summary>
/// Summary description for Handler1
/// </summary>
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
IList<Project_Data.User> usersList = Project_BLL.Users.ListUsers();
var data = new List<Project_BLL.Users>();
foreach (Project_Data.User user in usersList)
{
data = new List<Project_BLL.Users>{
new Project_BLL.Users {UserId = user.Id.ToString(), DisplayName = user.Name}
};
}
int limit = 10;
if (HttpContext.Current.Request.QueryString["limit"] != null)
limit = Convert.ToInt32(HttpContext.Current.Request.QueryString["limit"]);
string q = "";
if (HttpContext.Current.Request.QueryString["term"] != null)
q = HttpContext.Current.Request.QueryString["term"];
List<Project_BLL.Users> result = null;
var sb = new StringBuilder();
if (q.Trim() != "")
{
var query = data.Where(p => p.DisplayName.ToLower().Contains(q.ToLower()))
.OrderBy(p => p.DisplayName);
result = query.Take(limit).ToList();
foreach (var item in result)
sb.AppendFormat("{0}{1}|{2}", (sb.Length > 0 ? "\n" : ""), item.DisplayName, item.UserId);
}
context.Response.ContentType = "text/plan";
context.Response.Write(JsonConvert.SerializeObject(result.Select(u => new { id = u.UserId, value = u.DisplayName }).ToList()));
}
网络配置
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
<authentication mode="Forms">
<forms name="TestAuthCookie" loginUrl="login.aspx" timeout="1500">
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
<location path="images">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="App_Themes">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="js">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="css">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
</configuration>