2013-03-12 39 views
1

我有這個文本框和AutocompleteExtender如何將自定義值傳遞給AutocompleteExtender?

<asp:TextBox ID="txtItemName" runat="server" ClientIDMode="Static" 
MaxLength="300" onfocus="javascript:select();" 
></asp:TextBox> 
<cc1:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtItemName" EnableCaching="true" 
ServicePath="~/AutoComplete.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1" 
CompletionInterval="10" CompletionSetCount="15" FirstRowSelected="True" CompletionListCssClass="AutoExtender" 
CompletionListItemCssClass="AutoExtenderList" CompletionListHighlightedItemCssClass="AutoExtenderHighlight" 
> 
</cc1:AutoCompleteExtender> 

而且方法被定義爲

[WebMethod] 
public string[] GetCompletionList(string prefixText, int count, string contextKey) 
{ 
List<string> items = new List<string>(count); 
SqlCommand con = new SqlCommand(); 
SqlDataReader sdr = null; 
DataSet ds = new DataSet(); 
try 
{ 
    SqlCommand cmd = new SqlCommand(); 

    cmd.CommandText = "proc_NewBooking"; 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.AddWithValue("@BranchId", Globals.BranchID); 
    cmd.Parameters.AddWithValue("@ItemName", prefixText.ToString()); 
    cmd.Parameters.AddWithValue("@Flag", 11); 
    sdr = AppClass.ExecuteReader(cmd); 
    ds = AppClass.GetData(cmd); 
    while (sdr.Read()) 
    { 
     items.Add("" + sdr.GetValue(0)); 
    } 
    sdr.Close(); 
    sdr.Dispose(); 
} 
catch (Exception ex) 
{ 
      // Log the message and continue 
} 
return items.ToArray(); 
} 

我要的是通過自定義值,則documentation說,我們可以使用contextKey財產

做到這一點

因此,我在AutocompleteExtender中加入了這一行。

<cc1:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtItemName" EnableCaching="true" 
ServicePath="~/AutoComplete.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1" 
ContextKey="javascript:document.getElementById('drpCustomerType').value" 
CompletionInterval="10" CompletionSetCount="15" FirstRowSelected="True" CompletionListCssClass="AutoExtender" 
CompletionListItemCssClass="AutoExtenderList" CompletionListHighlightedItemCssClass="AutoExtenderHighlight" 
> 
</cc1:AutoCompleteExtender> 

和重載接受這個的方法如下,

public string[] GetCompletionList(string prefixText, int count, string contextKey) 
{ 
     // custom code based on context key 
} 

但是當調試器到達它,我沒有得到的drpCustomerType的值,而不是,我得到的硬編碼字符串「的javascript:文件.getElementById( 'drpCustomerType')。值「?

誰能告訴我如何將drpCustomerType的值傳遞給此方法,以便我可以根據該方法顯示項目列表?

回答

4

您需要在JavaScript處理OnClientPopulating事件和處理程序中設置contextKey屬性值:

function autoComplete1_OnClientPopulating(sender, args) { 
    sender.set_contextKey(document.getElementById('drpCustomerType').value); 
} 


<cc1:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtItemName" EnableCaching="true" 
ServicePath="~/AutoComplete.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1" 
UseContextKey="true" 
CompletionInterval="10" CompletionSetCount="15" FirstRowSelected="True" CompletionListCssClass="AutoExtender" 
CompletionListItemCssClass="AutoExtenderList" 
OnClientPopulating="autoComplete1_OnClientPopulating" 
CompletionListHighlightedItemCssClass="AutoExtenderHighlight" /> 
+0

沒有它不工作。 'autoComplete1_OnClientPopulating'或者說'sender.set_contextKey(document.getElementById('drpCustomerType')。value);'在firebug中永遠不會被命中。我甚至在此之後添加了警報,但它從未解僱。 – Razort4x 2013-03-12 11:48:28

+0

我的不好。我是在'OnClientPopulated'而不是'OnClientPopulating'中做的。 :-P 它的工作。 – Razort4x 2013-03-12 11:52:35

0
You can do it using Hidden Field, In Code Behind you didnot pass ContextKey anywhere 
Find the session and store it in hidden field at page load that will be your drpCustomerType 



    Do like this in WebMethod 

    [System.Web.Services.WebMethod (EnableSession=true)] 

     contextkey= Convert.ToInt32(HttpContext.Current.Session["drpCustomerType "].ToString()); 
     cmd.Parameters.AddWithValue("@drp ", contextkey); 

    @drp = Variable Declare in SP 
相關問題