0

我有一个通用处理程序,所以我可以使用 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>
4

2 回答 2

0

这个 web.config 中的 httpHandlers 部分在哪里?如果您希望您的处理程序工作,您必须在 web.config 中定义它

于 2012-04-26T14:44:14.570 回答
0

尝试更改 Source :

 $(function () {
        $("#user").autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: "handler1.ashx",
                    data: {
                        foo: request.term
                    },
                });
            },


            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);
            },

在这里您可以找到一个工作示例(查看源代码)

来自 JQuery 网站:

只需指定源选项,即可自定义自动完成以使用各种数据源。数据源可以是:

  • 包含本地数据的数组
  • 一个字符串,指定一个 URL
  • 回调_

您必须指定一个回调,使 Post / Get 到您的 ashx。

这里是 $.ajax 的完整参考

回调

于 2012-04-26T17:34:35.420 回答