2012-04-26 238 views
0

我有一個通用的處理程序,所以我可以使用jquery.ui Autocomplete,但處理程序不會觸發。Generic Handler not firing

繼承人我的代碼: 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> 
+0

完整引用你能告訴你的web.config? – 2GDev 2012-04-26 14:39:04

+0

只是加了它.. – Luis 2012-04-26 14:41:26

回答

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的網站:

只需指定源選項,即可自定義自動完成功能以使用各種數據源。數據源可以是:

  • 與本地數據
  • 一個字符串,指定URL
  • 一個回調

你必須指定一個回調,使一個帖子/數組去你的ashx。

Here爲$就

回調

+0

試過,但沒有工作.. – Luis 2012-05-02 09:01:14

+0

回答編輯看看... – 2GDev 2012-05-02 11:29:36

+0

我不知道該怎麼做,是從來沒有叫過不知道爲什麼,我有一個斷點來檢查它是否被調用,並且永遠不會輸入 – Luis 2012-05-02 14:19:01

0

哪裏是在這個web.config文件的HttpHandlers的部分?如果你希望你的處理工作,你必須在web.config中定義它

+0

或者使處理程序的端點成爲ASHX;實際上有一個Handler1是代碼隱藏的ASHX容器(不是真正的'頁面')。 – 2012-04-26 14:48:06

+0

我的處理程序是一個ashx文件 – Luis 2012-04-26 15:42:31

+0

只需在webconfig中設置此值,但給我一個錯誤: 錯誤:無法加載類型'Handler1'。 – Luis 2012-04-26 15:43:42