2012-09-11 21 views
0

我想在我的頁面中使用ASP.NET自動填充。我使用內部對象來加載數據,並且應該使用實例頁面字段值的參數來查詢自動完成。因爲我不能在靜態自動填充擴展方法中使用實例字段,所以我找到了一個sample of code,它可以用來實現我的目標。在代碼後面動態設置ASP.NET自動填充擴展器contextKey

我在asp.net頁面文本框中的自動完成和擴展:

<asp:TextBox ID="PurchaseOrderItemsSearchTextBox" runat="server" EnableViewState="False" 
          SkinID="SalesReturnTextBox" AutoCompleteType="Disabled" 
          AutoPostBack="True" OnTextChanged="PurchaseOrderItemsSearchTextBoxTextChanged" 
          onload="PurchaseOrderItemsSearchTextBoxLoad" /> 
<ajax:AutoCompleteExtender runat="server" TargetControlID="PurchaseOrderItemsSearchTextBox" ID="OrderOrStockNumberExtender" 
          ServiceMethod="GetOrderOrStockNumbers" MinimumPrefixLength="2" CompletionInterval="1000" UseContextKey="True"/> 

在PurchaseOrderItemsSearchTextBoxLoad方法我想通過javascript根據實施例注意到上述情況動態地設置上下文鍵。

protected void PurchaseOrderItemsSearchTextBoxLoad(object sender, EventArgs e) 
    { 
     var textBox = sender as TextBox; 
     if (textBox!=null) 
     {     
      string sapCode = Customer.Company.SapCode; 
      const string PURCHASE_ORGANIZATION = PURCHASE_ORGANIZATION_HP; 
      string purchaseGroup = string.Empty; 
      string firstDateTime = FirstDateTextBox.Text; 
      const string SEPARATOR = "#"; 
      var contextStrings = new[]{sapCode,PURCHASE_ORGANIZATION,purchaseGroup,firstDateTime}; 
      string context =string.Join(SEPARATOR, contextStrings); 
      const string ON_KEY_UP = "onkeyup"; 
      string attributevalue = "$find('" + OrderOrStockNumberExtender.ClientID + "').set_contextKey(" + context + 
            ");"; 
      textBox.Attributes.Add(ON_KEY_UP, attributevalue); 
     } 
    } 

但是當我運行我的網頁和靜態網頁的方法應返回字符串數組設置斷點,我發現contextKey爲空。

[WebMethod] 
[ScriptMethod] 
public static string[] GetOrderOrStockNumbers(string prefixText,int count, string contextKey) 
{    
    string[] returnStrings = new string[] {"1","2"}; 
    return returnStrings; 
} 

非常感謝您的任何有用的答案,

魯道夫。

回答

0

我找到了解決方案。由屬性值設置的ContextKey無法在ASP.NET中工作,因爲.NET 4.0在屬性值中存在轉義撇號,正如我在this文章中找到的。

一個解決方案是在Web配置中禁用編碼屬性值,但它可能會造成安全問題。

<httpRuntime encoderType="HtmlAttributeEncodingNot"/> 

我寧願改變我的做法並沒有設置在文本框控件的預渲染事件的屬性contextKey值,但註冊的客戶端腳本。

protected void PurchaseOrderItemsSearchTextBoxPreRender(object sender, EventArgs e) 
    { 
     var textBox = sender as TextBox; 
     if (textBox != null) 
     { 
      string sapCode = Customer.Company.SapCode; 
      const string PURCHASE_ORGANIZATION = PURCHASE_ORGANIZATION_HP; 
      string purchaseGroup = string.Empty; 
      string firstDateTime = FirstDateTextBox.Text; 
      var contextStrings = new[] {sapCode, PURCHASE_ORGANIZATION, purchaseGroup, firstDateTime}; 
      string context = string.Join(SEPARATOR, contextStrings); 
      const string ON_KEY_UP = "onkeyup"; 
      const string ON_KEY_UP_VALUE = "SetOrderOrStockExtenderContextKey();"; 
      textBox.Attributes.Add(ON_KEY_UP, ON_KEY_UP_VALUE); 

      //disable autocomplete in firefox 
      DisableAutocompleteInFirefox(textBox); 

      const string ORDER_OR_STOCK_NUMBER_SCRIPT_KEY = "OrderOrStockNumberScript"; 
      if (!ClientScript.IsClientScriptBlockRegistered(ORDER_OR_STOCK_NUMBER_SCRIPT_KEY)) 
      { 
       Type type = GetType(); 
       var scriptTextBuilder = new StringBuilder(); 
       const string FUNCTION_NAME = "function SetOrderOrStockExtenderContextKey()"; 
       scriptTextBuilder.AppendLine(FUNCTION_NAME); 
       const string LEFT_BRACKET = "{"; 
       scriptTextBuilder.AppendLine(LEFT_BRACKET); 
       const string FIND_COMMAND = "$find(\'"; 
       scriptTextBuilder.Append(FIND_COMMAND); 
       scriptTextBuilder.Append(OrderOrStockNumberExtender.ClientID); 
       const string SET_COTEXT_KEY = "\').set_contextKey('"; 
       scriptTextBuilder.Append(SET_COTEXT_KEY); 
       scriptTextBuilder.Append(context); 
       const string END_OF_COMMAND = "');"; 
       scriptTextBuilder.AppendLine(END_OF_COMMAND); 
       const string RIGHT_BRACKET = "}"; 
       scriptTextBuilder.AppendLine(RIGHT_BRACKET); 

       string setContextKeyFunction = scriptTextBuilder.ToString(); 

       ClientScript.RegisterClientScriptBlock(type, ORDER_OR_STOCK_NUMBER_SCRIPT_KEY, setContextKeyFunction, 
                 true); 
      } 
     } 
    } 

現在contextKey由Javascript正確設置,我可以在我的GetOrderOrStockNumbers方法中使用它的值。