2013-11-27 27 views
0

我想讓我的Ajax AutoCompleteExtender看起來像這樣 - http://jquery-sew.s3-website-us-east-1.amazonaws.com/。 建議列表將在括號中顯示代碼及其名稱。 例如:C01(瑞典)。當選擇一個項目時,只有代碼將顯示在文本框中。Jquery像C#中的AutoCompleteExtender一樣#

在這裏的幫助下,我能夠用兩個連接名稱獲得建議列表加載。現在卡在最後一部分。

這個想法是在item select上獲得回發,然後在服務器端執行字符串刪除操作。它是否正確。

編輯:

JAVASCRIPT:

function itemSelected(source, eventArgs) {    
    var hdnID = document.getElementById('<%= hdnValue.ClientID %>');  
    hdnID.value = eventArgs.get_value(); 
    __doPostBack(hdnId, ""); 
} 

ASPX:

<asp:hiddenfield id="hdnValue" onvaluechanged="hdnValue_ValueChanged" runat="server"/> 
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox> 
<asp:AutoCompleteExtender ID="ace" runat="server" TargetControlId="txtSearch" 
MinimumPrefixLength="2" EnableCaching="true" CompletionSetCount="10" 
CompletionInterval="0" ServiceMethod="GetCodes" UseContextKey="True" 
CompletionListCssClass="autoComplete" 
CompletionListItemCssClass="autoCompleteItem" 
CompletionListHighlightedItemCssClass="autoCompleteHL" 
onclientitemselected="itemSelected"> 
</asp:AutoCompleteExtender> 

ASPX.CS:

[System.Web.Script.Services.ScriptMethod()] 
[System.Web.Services.WebMethod] 
public static string[] GetCodes(string prefixText) 
{ 
    CountryBLL objCountry = new CountryBLL(); 
    return objDAL.GetCodes(prefixText); 
} 

protected void hdnValue_ValueChanged(object sender, EventArgs e) 
{ 
    //perform string remove action. 
} 

回發爲n不幸的是在這裏發病。

+0

你有jQuery插件或C#代碼的問題? – Patel

+0

它只是我不知道jQuery。 – Ruby

回答

2

您可以使用它下面所提及:

public string[] GetCodes(string prefixText) 
{ 
    return MyDB.tblCountries.Where(country=>country.CountryCode. 
     StartsWith(prefixText)).OrderBy(country=>country.CountryCode).Select(country => country.CountryCode + "(" + country.CountryName + ")").ToArray();  
} 

更新:

對於第二部分你可以更新你的文本框'txtSearch'如下圖所示:

<asp:TextBox ID="txtSearch" runat="server" onchange="ManageText(this); "></asp:TextBox> 

及以下所提到創建爲ManageText新的JavaScript方法:

function ManageText(textControl) { 
      textControl.value = textControl.value.replace(/ *\([^)]*\) */g, ""); 
     } 
+0

謝謝。這解決了我的第一部分。您能否就我提到的下一部分提出建議。 – Ruby

+0

你能用jquery發佈你的設計頁嗎? – SpiderCode

+0

我已更新我的問題 – Ruby

1

你可以有字符串[]使用LINQ如下

public string[] GetCodes(string prefixText) 
    { 
     return MyDB.tblCountries.Where(c=>c.CountryCode. 
      StartsWith(prefixText)).OrderBy(c=>c.CountryCode).Select(c=> c.CountryCode + "(" + c.CountryName + ")").ToArray();  
    } 
+0

謝謝。這解決了我的第一部分。您能否就我提到的下一部分提出建議。 – Ruby