2012-11-01 31 views
0

嘿我想在asp.net中使用自動完成jQuery在一個default1.aspx頁面上使用的控件。我的控件是在註冊文件夾中的searchinput.ascx。我的問題是我在searchinput控件的代碼文件上編寫了web方法(getmylist)。但該方法從未被調用過。誰能幫助我在asp.net控件上自動完成

+1

嘿,顯示一些代碼。 – Aristos

+0

你有這些數據註釋嗎? '[WebMethod]'和[ScriptMethod]' –

+0

是你的問題解決與我們的答案,然後不要忘記接受正確的答案.. –

回答

0

Jquery website

你可以找到笏你需要有一個開始。 此外,顯示您的ajax調用,以便我可以嘗試幫助它,它不工作。 你的方法寫作一個Web方法,並從jquery自動完成ajax調用應該工作得很好,否則。

0

它很難幫你沒有代碼,但一些常見的原因可能是:

  • 你沒有正確使用的ClientID值 - asp.net控件不具有實際加價爲同一ID他們在設計器中執行

  • 您的web方法有錯誤 - 您應該按f12打開您的web開發人員工具欄並轉至NET選項卡(至少在Firefox中)以查看500錯誤代碼或類似內容正在退回

0

創建Web方法如下所示:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public string[] GetPatientFirstName(string prefix) 
{ 
    List<string> customers = new List<string>(); 
    using (SqlConnection conn = new SqlConnection()) 
    { 
     string connectionstring = CCMMUtility.GetCacheForWholeApplication(); 
     conn.ConnectionString = connectionstring; 
     using (SqlCommand cmd = new SqlCommand()) 
     { 
      cmd.CommandText = "select distinct top(10) PatientFirstname from tblMessage where " + 
      "PatientFirstname like '%'+ @SearchText + '%' order by PatientFirstname"; 
      cmd.Parameters.AddWithValue("@SearchText", prefix); 
      cmd.Connection = conn; 
      conn.Open(); 
      using (SqlDataReader sdr = cmd.ExecuteReader()) 
      { 
       while (sdr.Read()) 
       { 
        customers.Add(string.Format("{0}", sdr["PatientFirstname"])); 
       } 
      } 
      conn.Close(); 
     } 
     return customers.ToArray(); 
    } 
} 

這裏是html代碼:

$(document).ready(function() { 
     $('[ID$=txtPatientFirstname]').live('keyup.autocomplete', function() { 

      $(this).autocomplete({ 
       source: function (request, response) { 
        $.ajax({ 
         url: '<%=ResolveUrl("~/Resources/WebService.asmx/GetPatientFirstName") %>', 
         data: "{ 'prefix': '" + request.term + "'}", 
         dataType: "json", 
         type: "POST", 
         contentType: "application/json; charset=utf-8", 
         success: function (data) { 
          response($.map(data.d, function (item) { 
           return { 
            label: item.split('-')[0], 
            val: item.split('-')[1] 
           } 
          })) 
         }, 
         error: function (response) { 
          alert(response.responseText); 
         }, 
         failure: function (response) { 
          alert(response.responseText); 
         } 
        }); 
       }, 
       select: function (e, i) { 
       }, 
       minLength: 1 
      }); 
     }); 
}); 

這是工作的例子......希望這能解決您的問題..