通用处理程序没有触发

时间:2012-04-26 13:47:24

标签: c# asp.net handler

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

    }

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>

2 个答案:

答案 0 :(得分:0)

此web.config中的httpHandlers部分在哪里?如果您希望处理程序工作,则必须在web.config

中定义它

答案 1 :(得分:0)

尝试更改来源:

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

here您可以找到一个有效的示例(查看来源)

来自JQuery网站:

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

  • 包含本地数据的数组
  • 一个String,指定一个URL
  • 回拨

您必须指定一个回复,以便发布/获取您的ashx。

Here $ .ajax

的完整参考

回调