0

jquery.ui オートコンプリートを使用できるように汎用ハンドラーがあるのですが、ハンドラーが起動しません。

Heres私のコード: 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()));

    }

Webconfig

<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サイトから:

source オプションを指定するだけで、オートコンプリートをカスタマイズして、さまざまなデータ ソースと連携させることができます。データ ソースには次のものがあります。

  • ローカル データを含む配列
  • URL を指定する文字列
  • コールバック

ashx への Post / Get を行うコールバックを指定する必要があります。

ここに $.ajax の完全なリファレンスがあります

コールバック

于 2012-04-26T17:34:35.420 に答える