Ajax Toolkit AutoComplete 几种用法
AutoComplete控件是微軟提供的 AJAX Control Toolkit 控件,是用來實現(xiàn)類似google 自動匹配和完成效果。
AutoComplete控件的用法很簡單,只需要頁面一個TextBox 和 一個 WebService方法就搞定,比寫一大堆的JS+.ashx處理省了一堆的時間。
首先WebService,WebService其實就是用來提供AutoComplete可以使用的數(shù)據(jù)的,它可以有兩種格式,一種是兩個參數(shù),另一個是三個參數(shù),它們的返回值均為string[]類型。
public string[] ServiceMethod (string prefixText, int count)
public string[] ServiceMethod (string prefixText, int count,string contextKey)
兩個參數(shù)的比較簡單,博客園也寫了一堆。
我們說下關(guān)于第三個參數(shù)的這個方法,contextKey 參數(shù),可以接受我們自定義傳值,比如有時候我用到了下拉框類別的時候,這個還是非常有用的 事例如下:
js:
<script language="javascript" type="text/javascript">
function txtKeyDown() {
var ddlGrpId = document.getElementById("<%=ddlGroupName.ClientID %>");
var aceId = "<%=AutoCompleteExtender1.ClientID %>";
var aceName = $find(aceId);
if (aceName != null)
aceName.set_contextKey(ddlGrpId.options[ddlGrpId.selectedIndex].value);
}
</script>
注意:
1.js 函數(shù)的最后一句,即在回傳之前,設(shè)置它的值。
?2.注意?$find(aceId) 和aceName.set_contextKey(ddlGrpId.options[ddlGrpId.selectedIndex].value) 中set_contextKey的方法一定不能錯,就如同它WebService方法的參
一樣名稱和方法的名稱可以自己隨便起,但參數(shù)名稱是規(guī)定的,錯了就夭折了哈。
aspx:
<td class="ControlTD" style="width: 200px; height: 19px;">
<asp:ScriptManagerProxy ID="ScriptManager2" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService/AutoComplete.asmx" />
</Services>
</asp:ScriptManagerProxy>
<asp:TextBox ID="txtPatient" runat="server" autoComplete="off" MaxLength="25" onkeydown="return txtKeyDown();" TabIndex="1" Width="194px"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" ServicePath="~/WebService/AutoComplete.asmx" ServiceMethod="GetPatients"
MinimumPrefixLength="1" TargetControlID="txtPatient" CompletionInterval="300" CompletionSetCount="5">
</ajaxToolkit:AutoCompleteExtender>
</td>
你要是看?onkeydown="return txtKeyDown();"?這句不爽,你也可以后臺注冊txtPatient.Attributes.Add("onkeydown", "return txtKeyDown()");
completionInterval:?是用戶錄入后多長時間, 程序去調(diào)用服務(wù)來獲取數(shù)據(jù)?單位是毫秒。
completionSetCount:指定一次應(yīng)該返回多少項 ?默認(rèn)10項
minimumPrefixLength:最少需要錄入多少長度才執(zhí)行匹配下拉。
WebService:
[WebMethod]
public string[] GetPatients(string prefixText,int count, string contextKey)
{
var selectUsers = new StringBuilder("select top 5 userName+'/'+userId as userinfo from ");
selectUsers.Append("userInfo where userType='0' and status='A' and grpId='"+contextKey+"' ");
selectUsers.Append(" and (userId like '" + prefixText + "%' or userName like '" + prefixText + "%')");
DataTable dt = dSqlHepler.ExeDataTable("text", selectUsers.ToString(), null, null, "", true);
dSqlHepler.Close();
if (dt != null && dt.Rows.Count > 0)
{
var items = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
items.SetValue(dr["userinfo"].ToString(), i);
i++;
}
return items;
}
else
{
var items = new string[1];
items.SetValue("No matching value", 0);
return items;
} }
以上簡單幾步全搞定。
沒搞定的是 ?在AutoCompleteExtender 下拉選項的事件控制,選擇一項觸發(fā)一個事件,有時候也很需要的。因為提供的沒有這個玩意,所以需要的話,只能費手腳的去下它的源碼修改
重新編譯了。你有更好的辦法?
轉(zhuǎn)載于:https://www.cnblogs.com/chehaoj/archive/2011/10/06/2200045.html
總結(jié)
以上是生活随笔為你收集整理的Ajax Toolkit AutoComplete 几种用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件开发过程(CMMI/RUP/XP/M
- 下一篇: 分组查询的相关说明